Given an array of Post objects, how might one initialize The Loop so that the common Loop functions could be used:
$posts = array( /* WP_Post, WP_Post, ... */);
while ( have_posts() ) {
the_post();
the_title();
}
I am aware that I could simply loop over the array elements and pass to each function the ID of each element, however for reasons beyond my control using the actual WordPress Loop is preferred here.
The array is provided from a function that I cannot alter. For purposes of discussion it might as well have come from unserialize().
2 Answers
I’m using this in one of my custom widgets:
global $post;
$posts = array( /* WP_Post, WP_Post, ... */);
while (list($i, $post) = each($posts)) :
setup_postdata($post);
// use the template tags below here
if(has_post_thumbnail()):
?><div class="featured_image_wrap"><?php
the_post_thumbnail();
?></div><?php
endif;
the_title();
endwhile;
// don't forget to restore the main queried object after the loop!
wp_reset_postdata();