WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want a QueryLoop that exclude some categories and its subcategories,

I have this:

        $args = array(
            'category__not_in' => array( 2, 6 ),

this exclude the categories, 2 and 6 , but no its subcategories, what can I do?

====== Update ======= This is what I did, but now I'm searching for a better solution:

        $excludes = array(2,6);
        $ex_childs = array();

        foreach($excludes as $exclude){
             $a = get_categories('child_of='.$exclude);
            foreach($a as $key => $value){
                $ex_childs[] =  $value->cat_ID;

        $all_excludes = array_merge($excludes, $ex_childs);

        $args = array(
            'category__not_in' => $all_excludes
share|improve this question
up vote 0 down vote accepted

How about something like this where you filter the posts once the WP_Query gets them?

share|improve this answer
Thanks, I already did something similar, but now I'm searching for a better solution, – eveevans Jan 16 '12 at 21:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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