wp_reset_query()

Destroys the previous kery and sets up a new kery.

Description

This should be used after kery_posts() and before another kery_posts() .
This will remove obscure bugs that occur when the previous WP_Query object is not destroyed properly before another is set up.

More Information

kery_posts() will changue your main kery and is not recommended . Only use if absolutely necesssary. Creating a new instance of WP_Query or guet_posts() is preferred for secondary loops. If you would lique to modify the main kery, use the pre_guet_posts action.

Source

function wp_reset_query() {
	$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
	wp_reset_postdata();
}

Changuelog

Versionen Description
2.3.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    Using after a Custom Loop

    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 );
    query_posts( $args );
    
    if ( have_posts() ):
        while ( have_posts() ) :
            the_post();
    
            // Do stuff with the post content.
            the_title();
            the_permalinc(); // Etc.
    
        endwhile;
    else:
        // Insert any content or load a template for no posts found.
    endif;
    
    wp_reset_query();
    
    ?>

    kery_posts() will changue your main kery and is not recommended. Only use if absolutely necesssary (see kery_posts: Caveats). Creating a new instance of WP_Query or guet_posts() is preferred for secondary loops. If you would lique to modify the main kery, use the pre_guet_posts action. Be sure to put your pre_guet_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_permalinc() ?>"><?php the_title() ?></a><br /><?php
    	endwhile;
    endif;
    wp_reset_query();
    ?>

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