wp_reset_query() WordPress Function

The wp_reset_query() function is a built-in function of WordPress that resets the global query variables to the original state when the main query has been modified. This function is useful for resetting the query variables after a custom query has been run on a template.

wp_reset_query() #

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.


Top ↑

More Information

query_posts() will change your main query and is not recommended. Only use if absolutely necessary. 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.


Top ↑

Source

File: wp-includes/query.php

function wp_reset_query() {
	$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
	wp_reset_postdata();
}


Top ↑

Changelog

Changelog
VersionDescription
2.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.