WordPress.org

Codex

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

Function Reference/rewind posts

This page is marked as incomplete. You can help Codex by expanding it.

Description

Rewind the loop posts in order to re-use the same query in different locations on a page.

Usage

<?php rewind_posts(); ?>

Parameters

This function does not accept any parameters.

Return Values

This function does not return any values.

Examples

// 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; ?>

Example with custom query

// main loop
<?php 
$args = array('posts_per_page' = -1);
$my_posts = new WP_Query($args);
if ($my_posts->have_posts()) : while ($my_posts->have_posts()) : $my_posts->the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>

// rewind
<?php $my_posts->rewind_posts(); ?>

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

Example on Author.php

 <?php get_header(); ?>
   <?php if (have_posts()): ?>
      <header>
       // initialize posts
        <?php the_post(); 
          _e('All Post by :'); echo get_the_author(); ?>
	  <?php if (get_the_author_meta( 'description' )): ?>
          <?php the_author_meta('description'); ?>
          <?php endif; ?>
      </header>
       // rewind posts
	<?php rewind_posts(); ?> 
   <?php while (have_posts()): the_post();
         endwhile; endif;?>
 <?php get_sidebar(); ?>
 <?php get_footer(); ?>

Change Log

Source File

rewind_posts() 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.