I have three loops on one page on my Wordpress website. Each loop does something different, but one of the loops (loop2) I would like to reuse at the bottom. So I try to rewind this loop right after the first time I used it, but that is not working for some reason, because the second time it won't loop again.
Has it something to do with the wp_reset_postdata
I call after each loop? What am I missing?
Here is my code:
$loop1 = new WP_Query(array(
// some args
));
if( $loop1->have_posts() ) {
while( $loop1->have_posts() ) { $loop1->the_post();
// do something
}
wp_reset_postdata();
}
$loop2 = new WP_Query(array(
// some args
));
if( $loop2->have_posts() ) {
while( $loop2->have_posts() ) { $loop2->the_post();
// do something
}
// REWIND this loop
$loop2->rewind_posts();
wp_reset_postdata();
}
$loop3 = new WP_Query(array(
//some args
));
if( $loop3->have_posts() ) {
while( $loop3->have_posts() ) { $loop3->the_post();
// do something
}
wp_reset_postdata();
}
// HERE WE GO: do the rewinded loop again
if( $loop2->have_posts() ) {
while( $loop2->have_posts() ) { $loop2->the_post();
echo 'Yes! It is working.';
}
wp_reset_postdata();
} else {
echo 'Nope :( Not working...';
}