Use the below example code to resetting post data to previous loop in nested loops:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | $publication = new WP_Query( array( 'connected_type' => 'publication_to_post', 'connected_items' => $post->ID, 'fields' => 'ids', 'posts_per_page' => 1, ) ); if ( $publication->have_posts() ) { while ( $publication->have_posts() ) : $publication->the_post(); echo '<h2>Publication title = '.get_the_title().'</h2>'; $pub_id = get_the_ID(); $issue = new WP_Query( array( 'connected_type' => 'publication_to_issue', 'connected_items' => $pub_id, 'fields' => 'ids', 'posts_per_page' => 1, ) ); if ( $issue->have_posts() ) { while ( $issue->have_posts() ) : $issue->the_post(); // need to be able to use template parts in here echo '<h2>Issue title = '.get_the_title().'</h2>'; endwhile; $publication->reset_postdata(); } echo '<h2>Publication title = '.get_the_title().'</h2>'; endwhile; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.