Title: Nested Loop with WP_Query won&#039;t work
Last modified: August 24, 2016

---

# Nested Loop with WP_Query won't work

 *  Resolved [webmistress666](https://wordpress.org/support/users/webmistress666/)
 * (@webmistress666)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/nested-loop-with-wp_query-wont-work/)
 * I have just a plain post (using single.php), and within that post, I want to 
   collect information from another post (via a given ID), in a different post type.
 * Here’s what I’ve got in single.php and functions.php:
 *     ```
       <?php
       // Our Outer Single Post Loop
       if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
       // Post Data Being Displayed Here
               // Get a Post ID and Pass it to the Function
               $post_id = 5;
               get_related_type($post_id);
       // Post Comments Go Here
       // End of Outer Loop.
       endwhile;
       ?>
       ```
   
 * functions.php
 *     ```
       function get_related_type($post_id) {
       	$args = array(
       	    'p' => $post_id, // id of a page, post, or custom type
       	    'post_type' => 'books',
       	    'posts_per_page' => 1);
               // New Inner Loop Query
       	$inner_query = new WP_Query($args);
       	while ($inner_query->have_posts()) :
       	?>
                       // Title of A Different Post
       		Title: <?php the_title(); ?>
       	<?php
       	endwhile;
       }
       ```
   
 * This just returns a few hundred iterations of the outer loop’s post title. I 
   can’t get it to only return 1 item, nor can I get it to actually return post_id#
   5’s title. I’ve tried resetting post data, and nothing changes.
 * I’ve been staring at this thing for so long, I’m sure it’s something really obvious,
   but I just can’t see it.
 * Any thoughts?

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/nested-loop-with-wp_query-wont-work/#post-5969009)
 * Why do you have the outer loop when you’re just getting the details by a single
   POST / Page ID?
 *  Thread Starter [webmistress666](https://wordpress.org/support/users/webmistress666/)
 * (@webmistress666)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/nested-loop-with-wp_query-wont-work/#post-5969013)
 * The outer loop displays the normal post. Within that normal post, I want to show
   data from another post.
 * Think of a shortcode that allows you to insert an excerpt of a book review into
   a blog post. The blog post is the normal post, the book review is a custom post
   type.
 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/nested-loop-with-wp_query-wont-work/#post-5969019)
 * Can you post the URL so we can see the output?
 *  Thread Starter [webmistress666](https://wordpress.org/support/users/webmistress666/)
 * (@webmistress666)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/nested-loop-with-wp_query-wont-work/#post-5969030)
 * Sure! But be careful, because this NEVER stops looping and could crash your browser
   unless you stop it loading.
 * [http://demo.angstyg.com/bugtest/2015/04/hello-world/](http://demo.angstyg.com/bugtest/2015/04/hello-world/)
 *  [Rajesh Soni](https://wordpress.org/support/users/rajeshsoni/)
 * (@rajeshsoni)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/nested-loop-with-wp_query-wont-work/#post-5969031)
 * Try this…
 *     ```
       <?php
       // Our Outer Single Post Loop
       if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
       // Post Data Being Displayed Here
               // Get a Post ID and Pass it to the Function
               $post_id = 5;
               echo "Title: " . get_the_title($post_id) . "<br/>";
       // Post Comments Go Here
       // End of Outer Loop.
       endwhile;
       ?>
       ```
   
 *  Thread Starter [webmistress666](https://wordpress.org/support/users/webmistress666/)
 * (@webmistress666)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/nested-loop-with-wp_query-wont-work/#post-5969034)
 * That does technically work, however I really need it to function without having
   to pass the ID into every tag. I’m using a plugin for custom fields that will
   be difficult to use if I have to pass the ID for every item.
 *  Thread Starter [webmistress666](https://wordpress.org/support/users/webmistress666/)
 * (@webmistress666)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/nested-loop-with-wp_query-wont-work/#post-5969052)
 * This is from the Codex:
 * [https://codex.wordpress.org/The_Loop#Nested_Loops](https://codex.wordpress.org/The_Loop#Nested_Loops)
 *     ```
       $my_query = new WP_Query( 'cat=3' );
       if ( $my_query->have_posts() ) {
       	while ( $my_query->have_posts() ) {
       		$my_query->the_post();
       		the_content();
       	}
       }
       wp_reset_postdata();
       ```
   
 * When I use that (specifying post ID instead of cat), I only get one iteration,
   which is good. But it’s still showing the outer loop’s post title, and not the
   inner loop’s.
 *  Thread Starter [webmistress666](https://wordpress.org/support/users/webmistress666/)
 * (@webmistress666)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/nested-loop-with-wp_query-wont-work/#post-5969057)
 * Then when I just stick it straight into the single.php, absolutely nothing happens.
 *     ```
       <?php /* The loop */ ?>
       <?php while ( have_posts() ) : the_post(); ?>
   
       	<?php get_template_part( 'content', get_post_format() ); ?>
       		<?php
       			$my_query = new WP_Query( 'p=5' );
       			if ( $my_query->have_posts() ) {
       				while ( $my_query->have_posts() ) {
       					$my_query->the_post();
       					echo 'Title:' . the_title();
       				}
       			}
       			wp_reset_postdata();
       		?>
       	<?php twentythirteen_post_nav(); ?>
       	<?php comments_template(); ?>
   
       <?php endwhile; ?>
       ```
   
 *  Thread Starter [webmistress666](https://wordpress.org/support/users/webmistress666/)
 * (@webmistress666)
 * [11 years, 2 months ago](https://wordpress.org/support/topic/nested-loop-with-wp_query-wont-work/#post-5969065)
 * Oh, I got it!
 * Turns out, you need to pass the post_type.
 * This is the one that works:
 *     ```
       $my_query = new WP_Query( array( 'post_type' => 'books', 'p' => $post_id ) );
       	if ( $my_query->have_posts() ) {
       		while ( $my_query->have_posts() ) {
       			$my_query->the_post();
       			echo 'Title: ';
       			the_title();
       		}
       	}
       	wp_reset_postdata();
       ```
   
 * Thanks for the help!

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Nested Loop with WP_Query won't work’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 2 participants
 * Last reply from: [webmistress666](https://wordpress.org/support/users/webmistress666/)
 * Last activity: [11 years, 2 months ago](https://wordpress.org/support/topic/nested-loop-with-wp_query-wont-work/#post-5969065)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
