66

Reading some stuff about query_reset_postdata and query_reset_query makes me confused. For example:

  1. Is there any need to use both wp_reset_postdata and wp_reset_query together?

  2. http://www.poststat.us/properly-reset-wordpress-query/

Above states that you should only use query_reset_postdata() when using "separate queries". In example2 there's a comment:

WP_Query( $args ) = wp_reset_postdata();

AND

query_posts ( $args ) = wp_reset_query();

And really you should never use wp_reset_query because you shouldn't use query_posts!?

In the WP Codex it states that you should use wp_reset_query() after a custom loop (first example) http://codex.wordpress.org/Function_Reference/wp_reset_query

Is the codex wrong then?

0

1 Answer 1

105

The difference between the two is that

  • wp_reset_query() - ensure that the main query has been reset to the original main query
  • wp_reset_postdata() - ensures that the global $post has been restored to the current post in the main query.

Indeed, looking at the source you'll see that the wp_reset_query() calls wp_reset_postdata(). The only difference between the two then is this line:

$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];

(in wp_reset_query()). So wp_reset_query() is only necessary should those two globals differ, and that only happens if query_posts() has been used somewhere.

When should I use them?

Simply put:

  • wp_reset_postdata() - immediately after every custom WP_Query()
  • wp_reset_query() - immediately after every loop using query_posts()

Should I use wp_reset_query

Well, yes, but it's only needed after using query_posts(). As you've pointed out you should never use query_posts(). So if you aren't ever using query_posts() then it's not necessary to call wp_reset_query() (instead of wp_reset_postdata().

In short, it's not that you shouldn't use wp_reset_query() instead of wp_reset_postdata(), it's that you shouldn't ever need to!

4
  • 1
    So basically you're saying: Only use wp_reset_postdata() and the codex has a poor example? (If you want to do it correct) Commented May 14, 2014 at 7:40
  • 2
    The codex may change, so I'm going to avoid making absolute statements about it which may become incorrect. It has an example a WP_Query() loop which uses wp_reset_query() and unnecessarily so. It could use wp_reset_postdata() instead, but no harm is done. It also has a query_posts() example where it does, and must, use wp_reset_query(). As noted in the codex, though this is a bad example and 'not recommended' in the sense that query_posts() should never be used. Commented May 14, 2014 at 8:08
  • Do I need to reset the query if I loop through the query within a foreach loop? For ex: $sol_args = array( 'post_type' => 'solution', 'orderby' => 'menu_order', 'order' => 'ASC', 'lang' => pll_current_language(), 'posts_per_page' => '-1', ); $solution_posts = new WP_Query($sol_args); $solution_posts = $solution_posts->get_posts(); <?php foreach ($solution_posts as $solution_post) { ?> <?php } ?> wp_reset_postdata() Commented Nov 9, 2022 at 13:01
  • I took this advice literally and had problems. It says to call wp_reset_postdata() immediately after every custom WP_Query() (emphasis added). But, [the developer reference][1] says that it should be called when: "A secondary query loop using $sec_query = new WP_Query() and $sec_query->the_post() affects the global $post variable." Again, emphasis added. If you are not calling the_post() then there is no need to call wp_reset_postdata() and, in fact, doing so can cause problems. [1]: developer.wordpress.org/reference/functions/wp_reset_postdata
    – Dave S
    Commented May 1 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.