the setup postdata example is :
<?php $posts = get_field('relationship_field_name'); if( $posts ): ?> <ul> <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?> <?php setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <span>Custom field from $post: <?php the_field('author'); ?></span> </li> <?php endforeach; ?> </ul> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?> <?php endif; ?>
With ACF Pro 5+ , when i begin a page with my field (outside the main loop) the code doesn’t work
it’s because you use $posts = get_field …
$posts is overidded and the main loop is broken
sometimes we need to use get_field outside the loop, to get datas from options pages
It would be cool to add a little notice for this particular case ?
calling $posts another way, like $relation_posts will work.
I will see what I can do about bringing this to the developer’s attention, but can’t guarantee anything will be done about it. Thanks for the feedback.
I’m not the writer or maintainer of the documentation. The following are just my own tjoughts.
Anywhere you see something like:
$posts = get_field('relationship_field_name'); it should be assumed that this code needs to be inside of “The Loop”. The documentation here is only a reference to how to use things in the most general way.
It would be extremely difficult for Elliot to include notes about all conditions that may arise, like putting the main WP loop inside a loop for a custom field. Something like this would cause errors and problems even without ACF where one post loop is nested inside another.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2023 Advanced Custom Fields.