Home › Forums › Front-end Issues › Nested Post Object Fields › Reply To: Nested Post Object Fields
The examples on this site are for the simplest use of each field so do not cover the nuances of nested post loops. This all comes down to understanding what I explained above, that wp_reset_postdata()
always resets the global $post
variable to the main query’s post and not to the previous query’s post. This is something that is not explained well in the WP documentation. Every case of nested post queries will be a little different, however, the same principle applies. When you have nested queries you cannot use setup_postdata($posts)
and wp_reset_postdata();
Instead you must do the work yourself rather than relying on WP to do the work for you. A quick example of showing the title for posts in a relationship field.
$related_posts = get_field('my-relationship-field');
if ($related_posts) {
foreach ($related_posts as $related_post) {
?><h1><?php echo get_the_title($related_post->ID); ?></h1><?php
}
}
This could also be done like this
$related_posts = get_field('my-relationship-field');
if ($related_posts) {
foreach ($related_posts as $related_post) {
?><h1><?php echo $related_post->post_title ?></h1><?php
}
}
Getting fields from a related post
$related_posts = get_field('my-relationship-field');
if ($related_posts) {
foreach ($related_posts as $related_post) {
$some_value = get_field('some-field', $related_post->ID);
}
}
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.