0

Essentially on my single.php file, I'm querying the current post directly into the template, while having also having a related posts section.

The issue is that when ACF tries to retrieve the field of the post inside the related post query loop, it retrieves the current displayed post instead.

while(have_posts()){ the_post();
    echo the_field('field1');
    echo the_field('field2');
    echo the_field('field3');
}

$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){ ?>
    <img src="<?php echo the_field('field1')?>">
<?php
    echo the_title();
 }

So essentially it grabs field1 from the current post instead of the recent_posts query. I've been very confused about this issue. the loop and query are out of the scope of the while loop, so it should be fine right?

0

I'm not fun of helper functions so I would write it like this:

$args=array(
    'post_type' => 'post',
    'posts_per_page' => '20',
    'post_status' => 'publish',
    'order'=>'DESC',
    'orderby'=>'ID',   
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();

        $current_id = $post->ID;
       echo the_field('field1', $current_id);

    }
    wp_reset_postdata();
} else {
    // no post
}
  • to be clear here, @drupalizeme stated use wp_reset_query, which is not what you want. However in his code he used wp_reset_postdata(); which IS correct. – rudtek Nov 10 '17 at 18:22
  • @rudtek I have misread the question and alter my original answer this is why this piece is left there :) Thank you – Drupalizeme Nov 10 '17 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.