WordPress.org

Ready to get started?Download WordPress

Codex

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

Function Reference/wp reset query

Description

wp_reset_query() restores the $wp_query and global post data to the original main query. This function should be called after query_posts(), if you must use that function. As noted in the examples below, it's heavily encouraged to use the pre_get_posts filter to alter query parameters before the query is made.

Usage

<?php wp_reset_query(); ?>

Parameters

This function does not accept any parameters.

Return Values

This function does not return any values.

Examples

The following example shows how to use wp_reset_query() after a custom loop. Note that the loop in the example is probably being used in addition to the main loop.

<?php

$args = array ( 'post_parent' => 5 );
$custom_query = new WP_Query( $args );

if ( $custom_query->have_posts() ):
    while ( $custom_query->have_posts() ) :
        $custom_query->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();

?>

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

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

Change Log

Source File

wp_reset_query() 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: 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.