WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp reset postdata

Description

Use this function to restore the context of the 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) 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 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.

Usage

<?php wp_reset_postdata(); ?>

Parameters

This function does not accept any parameters.

Return Values

This function does not return any values.

Examples

<?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 loop -->
	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
		<?php the_title(); ?>
		<?php the_excerpt(); ?>
	<?php endwhile; ?><!-- end of the loop -->

	<!-- put pagination functions here -->
	<?php wp_reset_postdata(); ?>

<?php else:  ?>

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

<?php endif; ?>

Notes

  • Uses: $wp_query

Change Log

Source File

wp_reset_postdata() is located in wp-includes/query.php.

Related

Articles

Code Documentation

  • Class: WP_Query - Detailed Overview of class WP_Query
  • Class: WP_Comment_Query - Class for comment-related queries
  • Class: WP_User_Query - Class for user-related queries
  • Object: $wpdb - Overview on the use of the $wpdb object
  • Function: set_query_var()
  • Function: get_query_var()
  • Function: query_posts() - Create additional custom query
  • Function: get_post() - Take an ID of an item and return the records in the database for that article
  • Function: get_posts() - A specialized function that returns an array of items
  • Function: get_pages() - A specialized function that returns an array of pages
  • Function: have_posts() - A condition that determines whether the query returned an article
  • Function: the_post() - Used to automatically set the loop after a query
  • Function: rewind_posts() - Clears the current loop
  • Function: setup_postdata() - Sets the data for a single query result within a loop
  • Function: wp_reset_postdata() - Restores the previous query (usually after a loop within another loop)
  • Function: wp_reset_query()
  • Function: is_main_query() - Ensures that the query that is being changed is only the main query
  • Action Hook: pre_get_posts - Change WordPress queries before they are executed
  • Action Hook: the_post - Modify the post object after query
  • Filter Hook: found_posts - Changes the value of the object found_posts WP_Query
See also index of Function Reference and index of Template Tags.