显示"相关帖子"基于ACF关系领域

时间:2016-07-01 14:35:44

标签: php mysql wordpress advanced-custom-fields

我想显示"相关帖子"具有名为' property'的自定义帖子类型的单个帖子页面将ACF关系字段用于其他自定义帖子类型。

其他帖子类型是' contact'在单一属性的帖子类型中,关系字段正在调用它。我一直在努力理解ACF's documentation here,但我无法理解为什么我的代码无法正常工作。

我需要根据经纪人显示相关属性。我不完全理解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'
            // )
        )
   ));

2 个答案:

答案 0 :(得分:0)

原因是混乱的原因是由于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'
      )
    )
));

答案 1 :(得分:0)

一个更复杂的查询,带有一个名为 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;