The scenario is, some posts have one category, ok. Some others have more than one category. I must create post with content and also slideshow thumbnails for use with Nivo plugin gallery. Ok.

I want to filter all the posts in the "slideshow" category when in index pages, so I use a simply

query_posts($query_string . '&cat=-1,-36');

Category number 1 is "Undefined", and number 36 is "Slideshow". Ok. But... I have some posts that have another category, lets say, number 50, "Applications" and I wanna show these posts, despite they are too in "Slideshow" category.

I cannot find the way to say WP "show me posts, except the ones in categories 1 and 36, but show me them if they have more categories".

Googling and reading a ton of docs and pages I haven't found any way to know the categories count by post (not how many posts by category), maybe to build a query like "show me that, filter that EXCEPT if count(categories are my post present) > 2 ).

Some ideas?

Thank you!

4 Answers 4


I think your logic is too complicated. Setting up query_posts() is essentially setting up MySQL query for database. No way query would be able to easily count amount of assigned categories on the fly and run conditions against that.

Let's flip your condition. Instead of posts that aren't in slideshow category alone we can do much simpler posts that belong to any category other than slideshow.

Try this:

$categories = get_categories( array( 'exclude' => '1,36' ) );
$include = array();

foreach ( $categories as $category )
    $include[] = $category->term_id;

query_posts( array( 'category__in' => $include ) );

PS I'd consider some other way to mark posts for slideshow (like meta field) and not mix it with categories altogether.


so you want a loop that will display posts that are in cat 50, but not in cat 36?

Not tried it, but could you do something like:

query_posts($query_string . '&cat=50,-36');

Failing that, you'll need all the post id's from the category you want to exclude:

$excluded_category = WP_Query($query_string . '&cat=36');
//array of just the post info
$excluded_posts = $excluded_category->posts;//might be 'post', and not 'posts'??
//get just the ID's of the post into an array
foreach ($excluded_posts as $post){
    $excluded_post_ids[] = $post->id;
//query for required category
$required_category = WP_Query($query_string . '&cat=50');

//start the loop
if($required_category->have_posts()):while ($required_category->have_posts()): $required_category->thepost();?>

//filter out unwanted category
if (!in_array(get_the_ID(),$excluded_post_ids):

//your loop here


this is from memory, so may need some debugging.

  • Obviously, you can sub out categories if I have got them the wrong way round.
    – Mild Fuzz
    Commented Sep 21, 2010 at 8:38
  • Also, you should be able to simple add multiple categories in the first line, to exclude several categories
    – Mild Fuzz
    Commented Sep 21, 2010 at 8:39

Another method would just be to use custom post types, rather than standard posts. You can use taxonomies to connect differing post types when required.


To build on Rarst's last comment, your slideshow posts would just belong to a single category (or none at all), and you would create a custom field and custom value for posts that are to be included in the slideshow. Then use 'meta_key' and 'meta_value' in your call to query_posts().


In this example, you create a custom field called 'includeInSlideshow' and write in 'true' in the value textbox in the write post area.

You can let WP's default query populate The Loop on your index page as normal.

Your Answer

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