繁体   English   中英

根据 ACF 关系字段显示“相关帖子”

[英]Display “Related Posts” based on ACF Relationship Field

我想使用名为“属性”的自定义帖子类型显示单个帖子页面的“相关帖子”,该帖子类型将 ACF 关系字段用于另一种自定义帖子类型。

另一个帖子类型是“联系人”,在“单一属性”帖子类型中,关系字段对此进行了调用。 我一直试图在这里了解ACF 的文档,但我无法真正理解为什么我的代码不起作用。

我需要根据经纪人显示相关属性。 我不完全理解 SQL 语句和表连接。

  $properties = get_posts(array(
        'post_type'         => 'property', // Page Custom Post Type
        'posts_per_page'    => 6,
        'meta_query'        => array(
            // 'relation'   => 'AND',
            // array(
                'key'       => 'contact', // Field name with 2nd custom post type, 'contact'
                'value'     => '"' . get_the_ID() . '"',
                'compare'   => 'LIKE'
            // )
        )
   ));

原来这搞砸的原因是 ACF 存储数组的方式。 他们的文档虽然适用于他们的案例,但由于嵌套数组而在我的文档中不可用。

这对我有用。

// This is the start of figuring out the array issue
$contact = get_field( 'contact' );
// This showed me the first array
$contact_array = $contact[0];
// This showed me the ACF array and allowed me to return the ID
$contact_ID = $contact_array->ID;

$properties = get_posts(array(
  'post_type'         => 'property',
  'posts_per_page'    => 6,
  'meta_query'        => array(
      'relation'      => 'AND',
      array(
        'key'         => 'contact',
        // Had to call the value like this as the array was nested like
        // a:2:{i:0;s:3:"123";i:1;s:3:"321";} or something.
        'value'   => '"' . $contact_ID . '"',
        'compare' => 'LIKE'
      )
    )
));

具有称为medienform 的附加分类法的更复杂的查询

//  Shortcode for ACF   -   Add Relationship ACF field    [sc_acf_fields] 
//
add_shortcode( 'sc_acf_fields', 'related_relationship' ); // Add your shortcode here
function  related_relationship() { 
// get the taxonomy medienform
$cat_taxonomies = get_terms( 'medienform', $args );  
$count = count($cat_taxonomies);  // wird nicht verwendet 
//
/// foreach category as $form_category  -  medienform
// 
foreach ( $cat_taxonomies as $form_category ):
        // check the arguments of the post_type, orderby
        $args =array(
                    'posts_per_page' => -1,
                    'post_type' => 'materialien',   // the CPT
                    'orderby' => 'title',           // menu_order                            
                    'order' => 'ASC',               // DESC                               
                    'tax_query' => array(           // the tax_query is important to list the posttypes to its taxonomies
                            'relation' => 'AND',  
                                   array(  
                                       'taxonomy' => 'medienform',
                                            'field' => 'slug',
                                            'terms' => $form_category->slug  
                                            )  
                                    ),
                            'meta_query' => array(  // the meta_query is important to list only the related posts of the key
                                array(                          
                                    'key' => 'post-relationship',       // name of custom field Relationship in: https://qualitaetsoffensive-teilhabe.de/wp-admin/post.php?post=12067&action=edit&classic-editor=1
                                    'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
                                    'compare' => 'LIKE'
                                )
                        ),
                    );  
        //
        // make an own WP_Query from the $args
        $materials = new WP_Query( $args ); 
        //
        // let´s give the category ->name here  and the term_link().    
        // actually a category should only be displayed when it has some child   //  here we build the accordeon
        // 
        if ($materials->have_posts() ) { 
                echo '<h4><a href="' . get_term_link( $form_category ) . '">' . $form_category->name . '</a></h4>';   
        };
        //
        // while schleife
        while ( $materials->have_posts() ) { 
            //
            //// use variable as the_post() query
           $materials->the_post();
            
            
      ?>  
            <div class="post-loop-single">
                <div class="thumbnail-img"><a href="<?php the_permalink(); ?>"><span><?php the_post_thumbnail('post-thumb'); ?> </span></a></div>
                <div class="post-loop">
                    <div class="post-loop-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
                </div>
            </div>
 <?php  }  wp_reset_query();

endforeach;
    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM