I am searching how to exclude posts from displaying in a loop if them are in a category x, but not in many categories... The Problem: if I do exclude posts i.e. out of category 5, all posts are listed in category 5 will be excluded. Also those which are also in category 3 and 1.

how do i exclude:

while (have_posts()) : the_post(); 
  if(in_category($myCatsToExcludeArray)) continue;
  ... some general outputs

1 Answer 1


All you'll need to do is count() the get_the_category() results to determine how many categories your post is in, and check it along with your in_category().

For example:

while ( have_posts() ) : the_post();    
if( in_category( $myCatsToExcludeArray ) && count( get_the_category( $post->ID ) ) == 1 ) {
do something

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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