[wp-trac] [WordPress Trac] #24785: Nested loops and resetting post data
WordPress Trac
noreply at wordpress.org
Wed Jul 17 16:06:23 UTC 2013
#24785: Nested loops and resetting post data
-------------------------+------------------------------
Reporter: ericlewis | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Query | Version:
Severity: normal | Resolution:
Keywords: |
-------------------------+------------------------------
Comment (by ericlewis):
In attachment:24785.diff, I suggest we copy the function wp_reset_postdata
into a method to the WP_Query class, so upon exiting a nested loop, this
method can be called on the previous query object. A la:
{{{
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
// Build custom query
$query = new WP_Query( array(
'posts_per_page' => 1
) );
// Custom query loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Build nested custom query
$subquery = new WP_Query( array(
'posts_per_page' => 2
) );
// Nested custom query loop
if ( $subquery->have_posts() ) {
while ( $subquery->have_posts() ) {
$subquery->the_post();
echo '<li>' . get_the_title() .
'</li>';
}
} else {
// no posts found
}
/* Restore custom query's post data */
$query->wp_reset_postdata();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata(); ?>
<?php endwhile; ?>
}}}
--
Ticket URL: <http://core.trac.wordpress.org/ticket/24785#comment:1>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list