ACF Post Objects: Data from two seperate post objects in one wp_query

I’m using Advaced Custom Fields and I have a custom post type which has two post object fields which are also custom post types. I’m trying to get Data from two related post objects within one wp_query. Getting Data from the first post object works fine, but for the second, all values are NULL. Doing to separate wp_queries would be a solution to get the right values, but I want to save all the Data in a specific array structure so it has to be all in the same loop. Thanks in advance for your help!

<?php if( $the_query->have_posts() ): ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); 

$post_object = get_field('buehne');

        if( $post_object ): 

            // override $post
            $post = $post_object;
            setup_postdata( $post ); 
            $buehne_titel = get_the_title(); //This is saved correctly

            wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly 
        endif; 

$post_object = get_field('band');

        if( $post_object ): 

            // override $post
            $post = $post_object;
            setup_postdata( $post ); 
            $band_titel = get_the_title(); //This is NULL
            $band_genre = get_field('genre'); //This is NULL
            $band_style = get_field('style'); //This is NULL

            wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly 
        endif; 
endwhile; endif; wp_reset_query(); 

Related posts

Leave a Reply

1 comment

  1. I’m not sure if this is the answer, but using wp_reset_postdata() inside The Loop can only cause problems. What I think is happening is that after successfully finding the first object, your program resets the main query back to the beginning, so that the Loop just goes back and does the same thing over again. Are you sure you’re not creating an infinite loop?

    If you put either wp_reset_postdata() or wp_reset_query() after the “endwhile;” statement, that may fix things. Sorry, but I don’t have time to try it myself with your WP environment. Good luck!