Destroys the previous query and sets up a new query.
Description
This should be used after query_posts() and before another query_posts() .
This will remove obscure bugs that occur when the previous WP_Query object is not destroyed properly before another is set up.
Source
function wp_reset_query() {
$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
wp_reset_postdata();
}
Changelog
Version | Description |
---|---|
2.3.0 | Introduced. |
Using after a Custom Loop
The following example shows how to use wp_reset_query() after a custom loop. Note that the loop in the example is probably being used in addition to the main loop.
query_posts() will change your main query and is not recommended. Only use if absolutely necessary (see query_posts: Caveats). Creating a new instance of WP_Query or get_posts() is preferred for secondary loops. If you would like to modify the main query, use the pre_get_posts action. Be sure to put your pre_get_posts filtering in your functions.php file.