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

So I'm looping with WP_Query and here is my code:

$country_posts = new WP_Query(get_direct_children($continent_cats));
if ($country_posts->have_posts())  : while ($country_posts->have_posts()) : $country_posts->the_post();

the get_direct_children function:

function get_direct_children($cat_name) {
        if (gettype($cat_name) == 'string') : $category_id = get_cat_ID($cat_name);
        elseif (gettype($cat_name) == 'integer') : $category_id = $cat_name;
        $args = array('parent'   => $category_id);
        $categories = get_categories($args);
        $cats = wp_list_pluck($categories,'cat_ID');
        $args = array (
              'category__and'                    => $cats
        return $args;

$country_posts->have_posts() returns true only if 1 category gets returned from get_direct_children. This happens when I turn off all the children of $continent_cats (which is a string by the way) except 1.

When only 1 child category is active:



array(1) { ["category__and"]=> array(1) { [0]=> string(2) "72" } }

So yay this is what I want it to do. have_posts() is returning true and looping my 1 active subcategory. HOWEVER, when I enable another child category, have_posts() returns false(I think, it's just not posting any content).

So this same function:



array(1) { ["category__and"]=> array(2) { [0]=> string(2) "72" [1]=> string(2) "71" } }

when I have 2 child categories active. Since this looks like correct syntax AND category__and is taking in the correct variable type, why doesn't it loop those 2 categories? Why does it not even loop 1 category?


share|improve this question
I guess there is no post which has these 2 categories. Your are using an AND here. – realloc Nov 3 '13 at 11:31
Do you want posts in both categories or posts in either or the categories? – s_ha_dum Nov 3 '13 at 15:52
either categories...oh man. thanks you 2. I didn't really think of the 'AND' as a logical statement. Works now. – blake Nov 3 '13 at 21:39


$args = array (
              'category__and'                    => $cats


$args = array (
                  'category__in'                    => $cats

Does the trick.

share|improve this answer

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.