Codex tools: Log in
Languages: English • Italiano • (Add your language)
Contents |
wp_reset_query() restores the $wp_query and global post data to the original main query. This function should be called after query_posts().
<?php wp_reset_query(); ?>
This function does not accept any parameters.
This function does not return any values.
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.
<?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(); ?>
wp_reset_query() is located in wp-includes/query.php.