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

Description Description

Source Source

File: wp-includes/query.php

function wp_reset_postdata() {
	global $wp_query;

	if ( isset( $wp_query ) ) {

Top ↑

Changelog Changelog

Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

    Example of secondary loop and reset

    // 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 -->
    	<!-- reset the main query loop -->
    	<?php wp_reset_postdata(); ?>
    <?php else:  ?>
    	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
  2. Skip to note content
    Contributed by Aurovrata Venet

    WARNING, only reset the post data if the query is successful…

    $query = array(
       //some post query parameters
    $post_results = get_posts($query);
      //do something with your query results
      //invoke post data reset here
    //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

You must log in before being able to contribute a note or feedback.