Skip to content

Instantly share code, notes, and snippets.

@melvinstanly
Created August 19, 2020 05:48
Show Gist options
  • Save melvinstanly/25d58d0c919eca9a73c56d8b3316bb84 to your computer and use it in GitHub Desktop.
Save melvinstanly/25d58d0c919eca9a73c56d8b3316bb84 to your computer and use it in GitHub Desktop.
WordPress post reset query
<?php
//https://wordpress.stackexchange.com/questions/144343/wp-reset-postdata-or-wp-reset-query-after-a-custom-loop
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.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment