rewind_posts()WP 1.5.0

Rewind the loop posts.

No Hooks.

Return

null. Nothing.

Usage

rewind_posts();

Examples

0

#1 Using one cycle 2 times

Example of how to apply rewind_posts() to use the same loop 2 times per page:

<?php 
// Use the loop for the first time
if( have_posts() ){
	while( have_posts()){ 
		the_post(); 
		?>
		<!--display posts -->
		<h2><?php the_title(); ?></h2>
		<?php
	}
}
?>

<!--any code -->

<?php 
// Use the loop a second time
// rewind the loop to the beginning to use `have_posts()` again
rewind_posts();

if( have_posts() ){
	while( have_posts() ){
		the_post(); 
		?>
		<!--display posts -->
		<h2><?php the_title(); ?></h2>
		<?php
	}
}
?>
0

#2 Example with custom query

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

Notes

  • Global. WP_Query. $wp_query WordPress Query object.

Changelog

Since 1.5.0 Introduced.

Code of rewind_posts() WP 6.0.1

function rewind_posts() {
	global $wp_query;
	$wp_query->rewind_posts();
}