How to reset custom WordPress queries

So today I wanted to go over a good little tip, which I always tend to use and would definitely recommend for any bespoke WordPress development you may be doing. When developing your website design, you might come across the need to use one of the following techniques.

Getting Started

There’s a couple of different functions you can use to access post data, these are:

  • wp_query()
  • query_posts()

Typically, the above two functions would be used when creating custom or multiple loops on any WordPress page. It’s worth noting, that there is one other method – this is default loop that you’ll see used on a template like ‘page.php’.

Let’s see what these loop examples look like when they’re used. You can also read more about wp_query()query_posts() and the_loop on the WordPress codex.

wp_query & wp_reset_postdata

wp_query is probably the most advanced usage of the three loops, allowing you to narrow down your search a lot more. I tend to use this out of the three ways, for that very reason. It’s not the easiest of the three, but definitely worth it.

Here’s an example of how it would be used:

$the_query = new WP_Query( $args );
 
while ( $the_query->have_posts() ) :
    $the_query->the_post();
    echo '<li>' . get_the_title() . '</li>';
endwhile;

So using that is fine, but the problem would occur if you were to add another custom loop on the same page, below or above this one. You may have a different set of arguments to set, meaning you are trying to access a different set of posts. If you don’t reset the post data, this easy task could become quite tricky. One simple line of PHP just after you loop will fix it:

wp_reset_postdata();

That’s all it takes to reset the post data, so that your other loops on the page can work as you imagine they should. Out of habit, I add this to the end of every wp_query loop I set, just to make sure!

query_posts & wp_reset_query

query_posts is probably the easiest way to access your posts, but in no means the best. This is all just my opinion of course, but I tend not to use this function.

The arguments you can pass through this function are limited, compared to wp_query and you don’t always get what you expect. Here’s how you’d use the function:

query_posts( $args );
 
while ( have_posts() ) : the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;

Like wp_query, this still sets post data which we need to escape if we want to use another loop on the page. We use a slightly different function, but in the same way as our first function.

wp_reset_query();

the loop & rewind_posts

This example is the standard loop, which you’ll see on most of the WordPress page templates. This simple loop uses not arguments and grabs the current post data, so is used to show page title, page content etc.

Here’s how you’d use it:

if ( have_posts() ) : while ( have_posts() ) : the_post();
echo '<li>' . get_the_title() . '</li>;

This does exactly what it says, rewinds the post data, so you can essentially use the same post, but maybe use different elements of it on separate blocks in your template.

Conclusion

So you can see, it’s quite an easy thing to do—reset post data. I’d definitely recommend you adding this to your loops, if you don’t already. It will save any glitches or anomalies in your loops that may cause you hours and hours of pain when trying to fix, whihc can be solved with a simple reset.