Title: wp_reset_query
Published: April 25, 2014
Last modified: February 24, 2026

---

# wp_reset_query()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#description)
 * [More Information](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#wp--skip-link--target)

Destroys the previous query and sets up a new query.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#description)󠁿

This should be used after [query_posts()](https://developer.wordpress.org/reference/functions/query_posts/)
and before another [query_posts()](https://developer.wordpress.org/reference/functions/query_posts/).

This will remove obscure bugs that occur when the previous [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)
object is not destroyed properly before another is set up.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#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`](https://developer.wordpress.org/reference/hooks/pre_get_posts/)
action.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#source)󠁿

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/query.php#L114)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/query.php#L114-L117)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_reset_postdata()](https://developer.wordpress.org/reference/functions/wp_reset_postdata/)`wp-includes/query.php` |

After looping through a separate query, this function restores the $post global to the current post in the main query.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.3.0](https://developer.wordpress.org/reference/since/2.3.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 2 content](https://developer.wordpress.org/reference/functions/wp_reset_query/?output_format=md#comment-content-845)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/wp_reset_query/#comment-845)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_reset_query%2F%23comment-845)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_reset_query%2F%23comment-845)
 4.  **Using after a Custom Loop**
 5.  The following example shows how to use [wp_reset_query()](https://developer.wordpress.org/reference/functions/wp_reset_query/)
     after a custom loop. Note that the loop in the example is probably being used 
     in addition to the main loop.
 6.      ```php
         <?php
     
         $args = array ( 'post_parent' => 5 );
         query_posts( $args );
     
         if ( have_posts() ):
             while ( have_posts() ) :
                 the_post();
     
                 // Do stuff with the post content.
                 the_title();
                 the_permalink(); // Etc.
     
             endwhile;
         else:
             // Insert any content or load a template for no posts found.
         endif;
     
         wp_reset_query();
     
         ?>
         ```
     
 7.  [query_posts()](https://developer.wordpress.org/reference/functions/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](https://developer.wordpress.org/reference/classes/wp_query/)
     or [get_posts()](https://developer.wordpress.org/reference/functions/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.
 8.      ```php
         <?php
         query_posts( 'post_parent=5' );
         if ( have_posts() ) :
         	while ( have_posts() ) : the_post();
         		?><a href="<?php the_permalink() ?>"><?php the_title() ?></a><br /><?php
         	endwhile;
         endif;
         wp_reset_query();
         ?>
         ```
     
 9.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_reset_query%2F%3Freplytocom%3D845%23feedback-editor-845)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_reset_query%2F)
before being able to contribute a note or feedback.