Exclude category WordPress (WP_Query not working)

Issue

Could anyone explain why this query isn’t working?
I want to exclude the posts tagged with homepage.
It still shows the post with category name ‘homepage’…

<?php
    $query = new WP_Query( 'category_name=-homepage');
?>

<?php if ( $query->have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <?php
            get_template_part( 'content', 'news' );
        ?>
    <?php endwhile; ?>
    <?php the_posts_navigation(); ?>
    <?php else : ?>
        <?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>

Solution

As given in the docs in case of excluding categories you have to use its ID and not slug (check here).

You could try:

$query = new WP_Query( array( 'category__not_in' => array( 11 ) ) );

Answered By – Grzegorz Pawlik

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published