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.

I have a custom post type (portfolio-type) with a custom hierarchical taxonomy (portfolio-category)

Now I would like to query only posts from potrfolio-type, so i do:

$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
query_posts( '&post_type=portfolio-type&paged=' . $paged );

And till here, everything works great. Now, I would like to query only posts, that are in any, of children custom taxonomy "categories" of a custom taxonomy category with given ID.

So I tried:

$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$query_args = array(
    'post_type' => 'portfolio-type',
    'cat' => $CAT_ID,
    'paged' => $paged,  
);
query_posts( $query_args );

Failed (no results), but I understand Why. those posts are not in standard category, but in custom taxonomy's category, so looking at wp Codex, this is what I came with:

$query_args = array(
    'post_type' => 'portfolio-type',
    'paged' => $paged,
    'tax_query' => array(
        'taxonomy' => 'portfolio-category',
        'field' => 'id',
        'terms' => $CAT_ID
    )
);

Failed again, but this time all custom posts from portfolio-type post_type displays (tax_query doesnt work, whatever ID i put there result is all the same)

What am I missing?

share|improve this question
    
sory, correcting, its ok over there ;) –  Marcin Bobowski Jan 18 '13 at 14:37
    
I mean I have it correctly in my code, and still it doesnt filter anything :( –  Marcin Bobowski Jan 18 '13 at 14:38
1  
I can see now, was not clear from you original question. Next hint from the docs: Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays)" - see codex.wordpress.org/Class_Reference/WP_Query - You don't have that. Wrap it inside another array(...), compare with the example there. - If something does not work as thought, check with the docs first, double-check if everything has been written properly. –  hakre Jan 18 '13 at 14:41
    
And that dear harke is a clue :). Please write as an answer. –  Marcin Bobowski Jan 18 '13 at 14:45
    
How about you answer your own question? The learning effect will be better for you and I must admit, I just left a comment and a link here, so write a nice answer your own, accept it and this makes it looking better then ;) –  hakre Jan 18 '13 at 15:00
add comment

1 Answer

The problem was in not reading documentation carefully enough ;)

As it stands in http://codex.wordpress.org/Class_Reference/WP_Query in 'tax_query' part, "tax_query takes an array of tax query arguments arrays" so the correct part of code would be:

$query_args = array(
    'post_type' => 'portfolio-type',
    'paged' => $paged,
    'tax_query' => array(
        array(
            'taxonomy' => 'portfolio-category',
            'field' => 'id',
            'terms' => $CAT_ID
        )       
    ),
);

The missing array() in existing array() was a problem. Thanks hakre :).

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.