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?
1 Answer
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,