0

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?

5
  • sory, correcting, its ok over there ;) Commented Jan 18, 2013 at 14:37
  • I mean I have it correctly in my code, and still it doesnt filter anything :( Commented Jan 18, 2013 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
    Commented Jan 18, 2013 at 14:41
  • And that dear harke is a clue :). Please write as an answer. Commented Jan 18, 2013 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
    Commented Jan 18, 2013 at 15:00

1 Answer 1

2

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 :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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