-1

I've got a page which displays the latest posts, but I'm trying to exclude posts from certain categories. This is my code:

<?php $categories = get_categories( $args ); ?>  
<?php
$args      = array(
    'numberposts' => 2,
    'post_status' => "publish",
    'post_type'   => "post",
    'orderby'     => "post_date",
    'exclude'     => "8"
);
$postslist = get_posts( $args );
echo '<div class="latest_new_posts">';
foreach ( $postslist as $post ) : setup_postdata( $post ); ?>
    <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
        <div class="blog-date-news"><span><?php the_date( 'd/m/Y' ); ?></span></div>
        <div class="blog-container">
            <div class="news-blog-title"><span><?php the_title(); ?></span></div>
            <div class="news-blog-excerpt"> <?php echo excerpt( 500 ); ?> </div>

        </div>
    </div>
<?php endforeach; ?>

So I'm trying to exlude category ID 8 from it but it's still showing. Any ideas what I'm doing wrong?

4

exclude in get_posts() refers to post IDs, they are not interchangeable with category IDs.

The argument you are looking for is probably 'category__not_in' => [ 8 ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.