Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress - don't display certain posts within the main loop

Tags:

wordpress

I'm trying to get WordPress to not display certain posts but it still seems to display the posts that I'm telling it not to. I currently have this code:

<?php
    if (have_posts()) : while (have_posts()) : the_post(); 
    if (in_array($post->ID, $_SESSION['save_array_posts'])) continue;
?>
<div class="yl_bg_post main_content videos">
    <h2 class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    <?php the_content('<p class="more">More></p>'); ?>
</div>
<?php endwhile; endif; ?>

And I'm trying to make WordPress not display certain posts that are stored within $_SESSION['save_array_posts']. The values are as follows:

array(5) { [0]=> int(190) [1]=> int(199) [2]=> int(63) [3]=> int(66) [4]=> int(68) }

WordPress seems to be looping through and still display them, I'm not quite sure why.


1 Answers

Not sure why you have this problem but you could try to exclude posts in the query like this and see if that solves your problem:

$myPosts = new WP_Query(array('post__not_in' => $_SESSION['save_array_posts']));

while ($myPosts->have_posts()) : $myPosts->the_post(); ?>
like image 77
user850010 Avatar answered May 27 '26 06:05

user850010



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!