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 called portfolio and a custom taxonomy called build-type (acting as categories)

I am trying to query portfolio posts by build-type ID e.g. all Portfolio posts in "Hotels" (id=4 for that taxonomy)

// gets the ID from a custom field to show posts on a specific page   
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array( 
    'post_type' => 'portfolio',
    'showposts' => -1,
    'tax_query' => array(
        'taxonomy' => 'build-type',
        'terms' => $buildType,
        'field' => 'term_id'
    ),
    'orderby' => 'title',
    'order' => 'ASC'
));

Currently it's calling all portfolio posts and not just those with the build-type ID

For 'field' => 'term_id' should I be using term_id, tag_ID, id or something else?

Anyone know how to get this working?

Thanks in advance!

share|improve this question
add comment

4 Answers

up vote 3 down vote accepted

The reason this isn't working is because 'tax_query' needs to be an array of arrays (confusing, I know).

...

'tax_query' => array(
    array(
        'taxonomy' => 'build-type',

...

It is that way so you can group a few different rules together.

share|improve this answer
    
Nice one Drew. That solved it. Posted the final script on Github gist.github.com/1275191 –  mattberridge Oct 10 '11 at 12:36
add comment

Drew was right, tax-query needs to be an array of arrays

The final solution is:

// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array( 
    'post_type' => 'portfolio',
    'showposts' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'build-type',
            'terms' => $buildType,
            'field' => 'term_id',
        )
    ),
    'orderby' => 'title',
    'order' => 'ASC' )
);

On github here:

https://gist.github.com/1275191

Thanks!

share|improve this answer
    
Unaccepting my answer and accepting your own really hurts man... hurts me in my heart. If anybody needs me, I'll be in the lounge. –  Drew Gourley Oct 19 '11 at 13:07
    
Hah sorry dude. I'm new to all this, didn't realise you could only have one answer. I will re-mark it so you can venture back out of the lounge –  mattberridge Oct 20 '11 at 10:28
    
Haha man it's alright, I was just joking around - If you want to self-accept an answer its totally allowed, but be sure to Upvote my answer so I at least get some point love. –  Drew Gourley Oct 20 '11 at 13:34
add comment

You need to create an array inside tax_query where you can also select operators. For example a print_r of tax_query should look like these.

 Array
(
    [relation] => AND
    [0] => Array
        (
            [taxonomy] => build-type
            [terms] => Array
                (
                    [0] => term1
                    [1] => term2blabla
                )

            [field] => slug
            [operator] => IN
        )

    [1] => Array
        (
            [taxonomy] => another-taxonomie
            [terms] => Array
                (
                    [0] => term1
                    [1] => term2
                )

            [field] => slug
            [operator] => IN
        )

)

Of course you can change the field for id but i always used slugs to keep it simpler. As you can see you can query multiple taxonomies like these.

share|improve this answer
add comment

you don't need to use the tax_query,remove it and append the following:

term=>build-type,'build-type=>hotels' for example.

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.