4

I want to exclude posts that only have the Uncategorized category (i.e. category id 1), but if a post is (erroneously) categorised as "Uncategorized" but also has other categories, I want to include it.

Every solution I've found so far excludes all posts with the Uncategorized category, whether they have other categories or not. I've tried:

'category__not_in' => array('1')

and

'cat' => -1

in the query arguments but I'm not getting the results I need. Is there a way around this?

2
  • 1
    Found it :-) Jun 2 '15 at 10:01
  • Thanks, upvoted there, but will also add my own answer as the final solution was subtly different. Jun 2 '15 at 10:24
8

Working off of Pieter Goosen's answer to this question, the way around this is to create a list of all the categories except the one you want to exclude, then search for posts that include them. That way, if a post has the excluded category but also other categories, it'll be included. So, in my case:

$args = array ('exclude'=>1,'fields'=>'ids');   
$exclude_uncategorized = get_terms('category',$args);

and then include the following in the $args for the wp_query:

'category__in' => $exclude_uncategorized,
1
  • 2
    I have looked through my and your approach, stupid me, forgot the taxonomy parameter in get_terms. Enjoy :-) Jun 2 '15 at 10:39

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