wp_reset_query() WP 2.3.0
Invalidates (destroys) the data of the last query created for use in an arbitrary WordPress Loop and restores the default loop data.
An arbitrary query (WordPress Loop) is performed by query_posts() function, which overwrites the global variable $wp_query
.
This should be used after query_posts() or if $posts or $wp_query variables have been used in the code. This will remove obscure bugs that occur when default WP_Query object was overwritten.
The function should be called immediately after an arbitrary loop and needs to all conditional tags work correctly and global variables correspond to the current page (query).
1 time = 0.000022s = very fast | 50000 times = 0.04s = speed of light
No Hooks.
Return
Null. Nothing.
Usage
wp_reset_query();
Examples
#1 Query data reset
This example shows how to use the function after an arbitrary Loop:
<?php query_posts('posts_per_page=5&category=6'); if( have_posts() ) : while( have_posts() ) : the_post(); ?> <a href="<?php the_permalink() ?>"><?php the_title() ?></a><br /> <?php endwhile; endif; wp_reset_query(); ?>
Notes
- Global. WP_Query. $wp_query WordPress Query object.
- Global. WP_Query. $wp_the_query Copy of the global WP_Query instance created during wp_reset_query().
Changelog
Since 2.3.0 | Introduced. |
Code of wp_reset_query() wp reset query WP 5.6
function wp_reset_query() {
$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
wp_reset_postdata();
}