Nested Loop with WP_Query won't 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)
Viewing 9 replies - 1 through 9 (of 9 total)
- The topic ‘Nested Loop with WP_Query won't work’ is closed to new replies.