0
votes

Building a Wordpress page template for a custom taxonomy and related custom post type. Within a new WP_Query I need to grab fields from (2) different ACF post object fields; list-staff and list-rep. Code works as expected up to the wp_reset_postdata(); correct amount of results are returned, data within each post is unique up to the point of the reset. After the reset, all data is the same within each post. Code follows, and I'm certain there's a more elegant solution:

<?php 
$args = array( 
'orderby' => 'title',
'order'   => 'ASC',
'post_type' => 'parade-of-homes',
'parade-category'   => 'parade-homes',
'posts_per_page'    => -1,
'meta_key'          => 'entry_number',
'orderby'           => 'meta_value',
'order'             => 'ASC'           
);
$listing = new WP_Query( $args );
if ( $listing->have_posts() ) :
    while ( $listing->have_posts() ) : $listing->the_post();
?>
<?php the_field('list_number'); ?>
<?php
$staff = get_field('list_staff');
$rep = get_field('list_rep');
if( $staff ): 
    // override $post
    global $post;
    $post = $staff;
    setup_postdata( $post ); 
    ?>
    <?php the_permalink(); ?><?php the_title(); ?>
    <?php 
    endif;
    if( $rep ): 
        // override $post
        $post = $rep;
        setup_postdata( $rep ); 
    ?>
    <?php the_field('mkt_co'); ?><?php the_field('mkt_tel'); ?>
    <?php
    endif;
    wp_reset_postdata();
    ?>
    <?php the_field('list_address') ?>
    <?php 
        endwhile; 
    endif;
wp_reset_query(); 
?>         
1

1 Answers

0
votes

Figured this one out. setup_postdata() is a completely wrong direction for this application. What is correct is documented on the ACF page for "displaying data for multiple post objects." As the article states "Using this method, the $post object is never changed so all functions need a second parameter of the post ID in question." Read more about it here; https://www.advancedcustomfields.com/resources/post-object/. My working code follows:

<?php 
    $args = array( 
    'orderby' => 'title',
    'order'   => 'ASC',
    'post_type' => 'parade-of-homes',
    'parade-category'   => 'parade-homes',
    'posts_per_page'    => -1,
    'meta_key'          => 'entry_number',
    'orderby'           => 'meta_value',
    'order'             => 'ASC'           
    );
    $listing = new WP_Query( $args );
    if ( $listing->have_posts() ) :
        while ( $listing->have_posts() ) : $listing->the_post();
?>
        <?php 
             the_field('list_number');
             $post_object = get_field('list_staff');
             if( $post_object ):
        ?>
             <a href="<?php echo get_permalink($post_object->ID); ?>">
             <?php echo get_the_title($post_object->ID); ?>
         <?php 
             endif; 
         ?>

         <?php 
             $post_object = get_field('list_rep');
             if( $post_object ):
         ?>
             <p><?php the_field('mkt_co', $post_object->ID); ?></span></p>
             <a href="tel:<?php the_field('mkt_tel', $post_object->ID); ?>"><?php the_field('mkt_tel', $post_object->ID); ?></a>
         <?php 
             endif; 
         ?>                    
         <?php the_field('list_address') ?>
     <?php 
         endwhile; 
    endif;
    wp_reset_query(); 
?>