rewind_posts() – what actually the use of it, and where using is required or preferred?

The incomplete Codex about this, says very simply:

rewind_posts():
Rewind the loop posts.

As per this WPSE thread, with Eugene Manuilov‘s answer, I got:

<?php
// fetch first post from the loop
the_post();

// get post type
$post_type = get_post_type(); 

// rewind the loop posts
rewind_posts();
?>

With Ian Stewart’s theme development tutorial, I found rewind_posts()‘s use in archive.php, category.php, tag.php, author.php:

<?php the_post(); ?>
<!-- echo page title -->
<?php rewind_posts(); ?>
<?php while ( have_posts() ) : the_post(); ?>
   <!-- echo content -->
<?php endwhile; ?>

But in TwentyThirteen theme we can’t see something like this, but a simple WordPress loop with conditional:

<?php if ( have_posts() ) : ?>
<!-- echo page title -->
<?php while ( have_posts() ) : the_post(); ?>
   <!-- echo content -->
<?php endwhile; ?>
<?php endif; ?>

So, I just want to know, while I have the WordPress loop to use, and that works with pagination also, then where do I need to REWIND THE LOOP, and why?

EDIT

Ok, after the first answer, I got a very good article describing the 3 Query-reset functions in WordPress:

» 3 Ways to Reset the WordPress Loop by Jeff Starr – DigWP.com

I hope with this the answer can be a lot more educative than currently what we got.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It generally the clears the current loop

// main loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>

// rewind
<?php rewind_posts(); ?>

// new loop
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>

Here it clears the main loop and start with the new loop

Reference: http://codex.wordpress.org/Function_Reference/rewind_posts

Method 2

It is actually not necessary if you use have_posts() in the loop since it is called at the end of the loop in said function:

public function have_posts() {
    if ( $this->current_post + 1 < $this->post_count ) {
        return true;
    } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
        /**
         * Fires once the loop has ended.
         *
         * @since 2.0.0
         *
         * @param WP_Query &$this The WP_Query instance (passed by reference).
         */
        do_action_ref_array( 'loop_end', array( &$this ) );
        // Do some cleaning up after the loop
        $this->rewind_posts();
    }

    $this->in_the_loop = false;
    return false;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x