There are a number of a methods to use to create another loop on the same page. So which one is the best to use? Let me explain each one and how they work.
Here are the options we have for “resetting the query”:
- rewind_posts()
- wp_reset_query()
- new WP_Query
How are these three different? Here’s the difference.
rewind_posts() reset the The Loop position for the particular instantiation of The Loop. wp_reset_query() resets the $wp_query main query. And new WP_Query() will let you keep the original query around in case you want to use it later on, but this will create a new query that you can “loop” through.
Here are some examples.
rewind_posts()
[sourcecode]
// First loop (get the last 10 posts in the "myVibe" category)
<?php query_posts(‘category_name=myVibe&showposts=10′); ?>
<?php while (have_posts()) : the_post(); ?>
<!– Do stuff… –>
<?php endwhile;?>
//loop reset
<?php rewind_posts(); ?>
//Second loop (Get all posts)
<?php while (have_posts()) : the_post(); ?>
<!– Do stuff… –>
<?php endwhile; ?>
[/sourcecode]
wp_reset_query()
Actual function:
[sourcecode]
function wp_reset_query() {
unset($GLOBALS['wp_query']);
$GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
}
[/sourcecode]
Example usage:
[sourcecode]
<?php $my_query = new WP_Query(‘category_name=special_cat&posts_per_page=10′); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<!– Do special_cat stuff… –>
<?php endwhile; ?>
<?php wp_reset_query();
<?php $my_query = new WP_Query(‘category_name=my_other_cat&posts_per_page=15′); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<!– Do special_cat stuff… –>
<?php endwhile; ?>
[/sourcecode]
Trac discussion of wp_reset_query()
new WP_Query
[sourcecode]
// going off on my own here
<?php $temp_query = clone $wp_query; ?>
<!– Do stuff… –>
<?php query_posts(‘category_name=special_cat&posts_per_page=10′); ?>
<?php while (have_posts()) : the_post(); ?>
<!– Do special_cat stuff… –>
<?php endwhile; ?>
<?php endif; ?>
// now back to our regularly scheduled programming
<?php $wp_query = clone $temp_query; ?>
[/sourcecode]
Some examples taken from codex.wordpress.org





I often use wp_reset_query() for ease. But I’ve seen some tricks using rewind_posts(), for example in Twenty Ten theme. The WP_Query() I think it does the same job as wp_reset_query(), so I rarely use it.
I try to solve this case:
First loop: get the last post in the “myVibe” category
Do some stuff
Second loop: get all posts in the “myVibe” category without repeating the last post displayed in first loop
Any ideas?
I would use something like offset in the query, that would probably do what you need it to. Look on the The_Loop codex page on codex.wordpress.org, it should be useful.