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 am trying to get all posts that belong to two or more taxonomies but I am failing...

So here is the pseudo of what I have.

taxonomy name = color-categories

I have 6 created items under color-categories:

red, blue, black, 2000, 3000, 4000

And each post will have 2 of those category combinations like (red,2000) or (blue,4000)...etc

But now let's say I want to pull all posts that are in 2000 and red only...how do I go about doing that?

This is the code I have thus far but not working:

$args = array(
    'post_type'         => 'item',
    'post_status'       => 'publish',
    'posts_per_page'    => -1,
    'tax_query'         => array(
        'relation'      => 'AND',
        array(
            'taxonomy'  => 'color-categories',
            'field'     => 'slug',
            'terms'     => 'red'
        ),
        array(
            'taxonomy'  => 'color-categories',
            'field'     => 'slug',
            'terms'     => '2000'
        )               
    )       
);

This results in nothing returned...

share|improve this question
    
Have you tried to use 'terms' => array( 'red', '2000' ) instead of two arrays and a relation in the 'tax_query'? –  Mike Madern Jan 11 '13 at 9:20
    
Yeah, I thought I tried that too but it didn't work the first time...now it does....strange... –  user23654 Jan 11 '13 at 14:55

1 Answer 1

up vote 0 down vote accepted

Here is the way I think it should work:

$args = array(
    'post_type'         => 'item',
    'post_status'       => 'publish',
    'posts_per_page'    => -1,
    'tax_query'         => array(
        array(
            'taxonomy'  => 'color-categories',
            'field'     => 'slug',
            'terms'     => array('red', '2000'),
            'operator'  => 'AND'
        )               
    )       
);

So you query posts which are in both red and 2000 using operator AND.

share|improve this answer
    
I could have sworn I have already tried this...but I did it again and it now works...thanks! –  user23654 Jan 11 '13 at 14:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.