Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
        endif;
        $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:

var_dump(get_direct_children($continent_cats));

returns:

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:

var_dump(get_direct_children($continent_cats));

returns:

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?

Thanks

share|improve this question
1  
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
add comment

1 Answer 1

Changing:

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

to:

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

Does the trick.

share|improve this answer
add comment

Your Answer

 
discard

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.