I want to exclude a category from showing in a function, but cannot figure out how to do it for my specific code as below. I want to make it so that any post that is in the "featured" category, does not display - but all other posts do.

$args = array( 'posts_per_page' => 4, 'category_name' => 'featured');                  
$last_5_posts_query = new WP_Query( $args );
while($last_5_posts_query->have_posts()) : 

I just cannot figure out where or how to add the exclude operator, which i assume is by adding a - somewhere. Thanks.

1 Answer 1


To exclude a category, you need to know its ID. You can then exclude it like this (please not the minus before the ID):

$args = array( 'posts_per_page' => 4, 'cat' => -the_category_id_to_put_in);  

The WP Query class reference has more information if you need it: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

  • Perfect, works exactly how i wanted it to. Apr 23, 2013 at 16:29
  • @NikkiMather Click the check mark to mark the answer that solved your problem as The Solution.
    – fuxia
    Apr 23, 2013 at 16:36

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.