0

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

2
  • Have you tried to use 'terms' => array( 'red', '2000' ) instead of two arrays and a relation in the 'tax_query'? Jan 11, 2013 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, 2013 at 14:55

1 Answer 1

1

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.

1
  • I could have sworn I have already tried this...but I did it again and it now works...thanks!
    – user23654
    Jan 11, 2013 at 14:55

Your Answer

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