Title: wp_reset_postdata
Published: April 25, 2014
Last modified: August 20, 2026

---

# wp_reset_postdata()

## In this article

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

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

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/wp_reset_postdata/?output_format=md#more-information)󠁿

Use this function to restore the context of the [template tags](https://developer.wordpress.org/themes/basics/template-tags/)
from a secondary query loop back to the main query loop.

Differences between the main query loop and secondary query loops are:

 * the main query loop is based on the URL request and is initialised before theme
   templates are processed
 * secondary query loops are queries (using new [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/))
   in theme template or plugin files

A secondary query loop using` $sec_query = new WP_Query()` and `$sec_query->the_post()`
affects the global `$post` variable. The global `$post` variable is used by [template tags](https://developer.wordpress.org/themes/basics/template-tags/)
by default. `wp_reset_postdata()` restores the global `$post` variable to the current
post in the main query (contained in the global `$wp_query` variable as opposed 
to the `$sec_query` variable), so that the template tags refer to the main query
loop by default again.

Example

    ```php
    <?php
    $args = array( 'posts_per_page' => 3 );

    // the query
    $sec_query = new WP_Query( $args );
    ?>

    <?php if ( $sec_query->have_posts() ) : ?>

    <!-- start of the loop. the_post() sets the global $post variable -->
    <?php while ( $sec_query->have_posts() ) : $sec_query->the_post(); ?>

    <!-- template tags will return values from the post in the $sec_query object
    <?php the_title(); ?>
    <?php the_excerpt(); ?>

    <?php endwhile; ?><!-- end of the loop -->

    <?php else: ?>

    <?php _e( 'Sorry, no posts matched your criteria.' ); ?>

    <?php endif; ?>

    <!-- reset global post variable. After this point, we are back to the Main Query object -->
    <?php wp_reset_postdata(); ?>
    ```

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

    ```php
    function wp_reset_postdata() {
    	global $wp_query;

    	if ( isset( $wp_query ) ) {
    		$wp_query->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/7.1/src/wp-includes/query.php#L127)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/query.php#L127-L133)

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

| Uses | Description | 
| [WP_Query::reset_postdata()](https://developer.wordpress.org/reference/classes/wp_query/reset_postdata/)`wp-includes/class-wp-query.php` |

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

  |

| Used by | Description | 
| [WP_REST_Revisions_Controller::restore_post_data()](https://developer.wordpress.org/reference/classes/wp_rest_revisions_controller/restore_post_data/)`wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php` |

Restores the global post to its previous value after preparing a revision.

  | 
| [wp_dashboard_recent_posts()](https://developer.wordpress.org/reference/functions/wp_dashboard_recent_posts/)`wp-admin/includes/dashboard.php` |

Generates Publishing Soon and Recently Published sections.

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

Destroys the previous query and sets up a new query.

  |

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

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

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

 1.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/wp_reset_postdata/?output_format=md#comment-content-532)
 2.    [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/wp_reset_postdata/#comment-532)
 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_postdata%2F%23comment-532)
     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_postdata%2F%23comment-532)
 4.  **Example of secondary loop and reset**
 5.  **Note**: If you use [the_post()](https://developer.wordpress.org/reference/functions/the_post)
     with your query, you need to run [wp_reset_postdata()](https://developer.wordpress.org/reference/functions/wp_reset_postdata)
     afterwards to have template tags use the main query’s current post again.
 6.      ```php
         <?php
         // example args
         $args = array( 'posts_per_page' => 3 );
     
         // the query
         $the_query = new WP_Query( $args );
         ?>
     
         <?php if ( $the_query->have_posts() ) : ?>
     
         	<!-- start of the secondary loop -->
         	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
         		<?php the_title(); ?>
         		<?php the_excerpt(); ?>
         	<?php endwhile; ?>
         	<!-- end of the secondary loop -->
     
         	<!-- put pagination functions here -->
     
         <?php else:  ?>
     
         	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
     
         <?php endif; ?>
         <!-- reset the main query loop -->
         <?php wp_reset_postdata(); ?>
         ```
     
 7.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_reset_postdata%2F%3Freplytocom%3D532%23feedback-editor-532)
 8.   [Skip to note 5 content](https://developer.wordpress.org/reference/functions/wp_reset_postdata/?output_format=md#comment-content-2325)
 9.    [Aurovrata Venet](https://profiles.wordpress.org/aurovrata/)  [  9 years ago  ](https://developer.wordpress.org/reference/functions/wp_reset_postdata/#comment-2325)
 10. [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_postdata%2F%23comment-2325)
     Vote results for this note: 0[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_postdata%2F%23comment-2325)
 11. WARNING, only reset the post data if the query is successful…
 12.     ```php
         $query = array(
            //some post query parameters
           );
         $post_results = get_posts($query);
         if(!empty($post_results)){
           //do something with your query results
           //invoke post data reset here
           wp_reset_postdata();
         }
         //if you invoke it after the check and the result did not return any posts, it will reset the post data from a previous query
         wp_reset_postdata(); // WRONG
         ```
     
 13.  * This is only true if someone overrides the main `global $wp_query` variable–
        which I would encourage you NOT to do unless you are really sure of what you
        are doing in the first place. Use a seporate variable to store your custom 
        queries when you make them.
      * [williampatton](https://profiles.wordpress.org/williampatton/) [6 years ago](https://developer.wordpress.org/reference/functions/wp_reset_postdata/#comment-4158)
 14.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_reset_postdata%2F%3Freplytocom%3D2325%23feedback-editor-2325)
 15.  [Skip to note 6 content](https://developer.wordpress.org/reference/functions/wp_reset_postdata/?output_format=md#comment-content-6443)
 16.   [gorebatschov](https://profiles.wordpress.org/gorebatschov/)  [  4 years ago  ](https://developer.wordpress.org/reference/functions/wp_reset_postdata/#comment-6443)
 17. [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_postdata%2F%23comment-6443)
     Vote results for this note: 0[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_postdata%2F%23comment-6443)
 18. As I have just learned, there is a long-standing **bug** (reported 12 years ago)
     that results in `wp_reset_postdata()` not working properly in the admin panel.
 19. In my case, metabox values were not saved on a specific page with a custom `WP_Query`
     even though I was calling `wp_reset_postdata()` afterwards. The error message 
     I had (visible through the browser console -> network tab) was:
      `400: A post 
     ID mismatch has been detected.`
 20. I hope that this comment saves someone a few hours digging…
 21. More info & working workaround:
      [Can’t wp_reset_postdata after custom WP_Query in an admin edit page](https://core.trac.wordpress.org/ticket/18408)
 22.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_reset_postdata%2F%3Freplytocom%3D6443%23feedback-editor-6443)

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