简体   繁体   中英

Display “Related Posts” based on ACF Relationship Field

I want to display "Related posts" of a single post page with custom post type named 'property' which is using the ACF Relationship Field for another custom post type.

That other post type is 'contact' and in the Single Properties post type, the relationship field is calling out for that. I have been trying to understand ACF's documentation here , but I was not able to really comprehend why my code isn't working.

I need to show related properties based on the brokers. I don't fully understand SQL statements and table joining.

  $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'
            // )
        )
   ));

Turns out the reason that this was messed up was due to the way ACF stored the array. Their documentation, while it works for their case, was off in mine due to a nester array.

This is what worked for me.

// 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'
      )
    )
));

A more complex query with an additional taxonomy called 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;
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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