WordPress at a glance

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).

  • Global. WP_Query. $wp_query Global WP_Query instance.
  • Global. WP_Query. $wp_the_query Copy of the global WP_Query instance created during wp_reset_query().
✈ 1 time = 0.000022s = very fast | 50000 times = 0.04s = speed of light

No Hooks.

Return

Nothing (null).

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();
?>

Code of wp_reset_query: wp-includes/query.php VER 4.9.8

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

Related Functions

From tag: reset

More from tag: query

More from category: Queries

No comments
    Hello, !     Log In . Register