I’m not sure if there is an error in my syntax, but for some reason if I insert the following PHP code, then it causes the ACF field below it
<?php the_field('fieldname'); ?> to disappear from the content entirely (like not even appear in the html code). If I paste it below the ACF field it’s fine, only if it is above does this issue occur.
<?php $args = array( 'post_type' => 'post', 'numberposts' => -1); $lastposts = get_posts( $args ); foreach ( $lastposts as $post ): setup_postdata( $post ); ?> <div class="col"> <p class="ingress">“<?php the_field('review'); ?>”</p> <img src="<?php the_field('star_rating'); ?>" class="star-rating"><br> <img src="<?php the_field('image'); ?>" class="avatar"> <p class="name-location"><?php the_field('name_location'); ?></p> </div> <?php endforeach; ?>
setup_postdata() modifies the global variables for the
$post. This sets the post to a different one in the loop when you run
setup_postdata(). After your
endforeach; you should have
wp_reset_postdata(); to put the global variables back to where they were before your foreach loop.
See the docs for more information on setup_postdata
Here is their description of what happens which is probably better worded than mine.
setup_postdata() does not assign the global $post variable so it’s important that you do this yourself. Failure to do so will cause problems with any hooks that use any of the above globals in conjunction with the $post global, as they will refer to separate entities.