Home › Forums › General Issues › Post_object inside loop
Hi
When trying to display a custom post type with post_object inside a loop, I only get the first post. Example of my code:
<?php
$your_posts = new WP_Query( $args );
// Loop starts here
if ( $your_posts->have_posts() ) : while ( $your_posts->have_posts() ) : $your_posts->the_post();
// First post from a CPT
if( get_field('custom_post') ) {
$post_object = get_field('custom_post');
if( $post_object ) {
$post = $post_object;
setup_postdata( $post );
?>
<a href="<?php the_permalink(); ?>"><?php if(get_field('CPT_name')) { the_field('CPT_name'); } ?></a>
<?php
wp_reset_postdata();
}
}
// Second post from a CPT
if( get_field('custom_post2') ) {
$post_object = get_field('custom_post2');
if( $post_object ) {
$post = $post_object;
setup_postdata( $post );
?>
<a href="<?php the_permalink(); ?>"><?php if(get_field('CPT_name')) { the_field('CPT_name'); } ?></a>
<?php
wp_reset_postdata();
}
}
endwhile; else: ?><p>Sorry, there are no posts to display</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
So the first post will be displayed, while the next will not.
This works on a single page, but when I do it in a loop with multiple posts and try to get their post_objects, it only works for the first.
I want to note that if I remove the first “custom_post”, it will display “custom_post2”.
Also, should I use
<?php wp_reset_postdata(); ?>
Instead of
<?php wp_reset_query(); ?>
In the end?
So, here’s you problem.
You have a main WP query, and this loops over the posts.
Then you have your secondary query
$your_posts = new WP_Query( $args );
// Loop starts here
if ( $your_posts->have_posts() )
then you are referring to another post.
Here’s the common misconception, that wp_reset_postdata(), or any of the reset functions. This will not reset the post data to whatever was in it previously. This call always resets the post to the post in the main WP query. I explained this in more detail here https://support.advancedcustomfields.com/forums/topic/nested-post-object-fields/#post-48227
When you’re nested more than 2 deep you can no longer safely use any of the WP reset functions.
Instead of this
if( $post_object ) {
$post = $post_object;
setup_postdata( $post );
?>
<a href="<?php the_permalink(); ?>"><?php if(get_field('CPT_name')) {
the_field('CPT_name'); } ?></a>
<?php
wp_reset_postdata();
}
you need to do something like this
if( $post_object ) {
?>
<a href="<?php
echo get_permalink($post_object->ID); ?>"><?php
if (get_field('CPT_name', $post_object->ID)) {
the_field('CPT_name', $post_object->ID);
} ?></a>
<?php
}
Hi John
Thank you for your answer. Unfortunately it didn’t solve my problem adding the $post_object->ID. I read the post you linked to and I understand the problem. You suggest to use alternate means of getting values other than setup_postdata, can you give any examples of this?
Okay, so I got it working by using different post_object names and by removing the wp_reset_postdata();
<?php
$your_posts = new WP_Query( $args );
// Loop starts here
if ( $your_posts->have_posts() ) : while ( $your_posts->have_posts() ) : $your_posts->the_post();
// First post from a CPT
if( get_field('custom_post') ) {
$post_object_cpt = get_field('custom_post');
if( $post_object_cpt ) {
$post_cpt = $post_object_cpt;
setup_postdata( $post_cpt );
?>
<a href="<?php the_permalink($post_object_cpt->ID); ?>"><?php if(get_field('CPT_name', $post_object_cpt->ID)) { the_field('CPT_name', $post_object_cpt->ID); } ?></a>
<?php
}
}
// Second post from a CPT
if( get_field('custom_post2') ) {
$post_object_cpt2 = get_field('custom_post2');
if( $post_object_cpt2 ) {
$post_cpt2 = $post_object_cpt2;
setup_postdata( $post_cpt2 );
?>
<a href="<?php the_permalink($post_object_cpt2->ID); ?>"><?php if(get_field('CPT_name', $post_object_cpt2->ID)) { the_field('CPT_name', $post_object_cpt2->ID); } ?></a>
<?php
}
}
endwhile; else: ?><p>Sorry, there are no posts to display</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
Can anyone please tell me if this is the correct way of doing it?
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!
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.