Save (and exclude) posts from loop to use in another loop on page

I’m trying to display related posts on my taxonomy page, but not sure how to work out the second loop. I have shared taxonomys among posts, and on this page it shows post type A with tags “Light”.

Some of the posts with tags “Light” are NOT post type A. So I have the following:

<?php $related = array(); ?>
<?php while (have_posts()) : the_post(); ?>
<?php if(get_post_type() == ‘Type_B’ || get_post_type() == ‘Type_C’){
array_push($related, $post);
continue;
} ?>
<?php endwhile; ?>

This works fine. But then later down on the page where I want to loop type B and C I thought it would be simple to make a similar loop. But the wp_query array seems to be special? Even though my array contains posts.

if($related->have_posts()){
while($related->have_posts()) { $related->the_post();
print_r($post);
}
}

I get the following error: Fatal error: Uncaught Error: Call to a member function have_posts() on array

I’ve tried $relatedList = new WP_query($related); but it’s not right. Any tips?

Read more here:: Save (and exclude) posts from loop to use in another loop on page

Leave a Reply

Your email address will not be published. Required fields are marked *