I have a post list on my homepage that is displaying all posts in chronological order (DESC). I want to exclude posts from a specific category from this list. How do I go about this? My query is…
<ul class="home-news"><?php
$args = array( 'numberposts' => 5, 'order'=> 'DESC', 'orderby' => 'post_date' );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<li>
<a href="<?php the_permalink() ?>">
<?php the_title(); ?>
<span>Posted on <?php the_date(); ?></span>
</a>
</li>
<?php endforeach; ?>
</ul>
Solution 1
Add the following to your
argsarray:Where
idis the category id of the category you want to exclude. This solution will not diminish the number of posts requested.Solution 2
Add the following at the beginning of the inside of the
foreachloop:Note that if posts have multiple categories, you’d want to loop through the
$categoryarray and check each element.