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.

Below is my tax query, at the moment this returns all posts which have at least one of the specified terms.

Is there a way I can have it return only posts which have ALL of the specified terms?

[tax_query] => Array
    (
        [relation] => AND
        [0] => Array
            (
                [taxonomy] => services
                [field] => term_id
                [terms] => Array
                    (
                        [0] => 3
                        [1] => 10
                        [2] => 6
                        [3] => 9
                    )

                [operator] => IN
            )

        [1] => Array
            (
                [taxonomy] => awards
                [field] => term_id
                [terms] => Array
                    (
                        [0] => 13
                        [1] => 12
                    )

                [operator] => IN
            )

    )
share|improve this question

2 Answers 2

Try [operator] => AND in both the sub-arrays. I think that will work from my reading of WP_Tax_Query::get_sql().

share|improve this answer
    
That worked thanks! –  Frank Martin Aug 5 '11 at 11:55
    
That's good to know. I actually should be programming something similar right now. :-) Cheers. –  Chris Carson Aug 5 '11 at 12:01
    
And case is important (at least on WP bloody Engine -- no problem on Fedora+MariaDB). 'AND', not 'and'. grrrrr –  webaware Mar 7 at 9:29

Alternately, you could break the two queries into five (depending on how you're getting your conditions, it might even be easier to code).

It would be interesting to know whether there's any performance difference between the two constructions. (@Frank Martin, or anyone, it would be great if you could share the actual SQL generated each way - I set this answer as a community wiki.)

[tax_query] => Array
(
    [relation] => AND
    [0] => Array
        (
            [taxonomy] => services
            [field] => term_id
            [terms] => Array
                (
                    [0] => 3
                )

            [operator] => IN
        )
    [1] => Array
        (
            [taxonomy] => services
            [field] => term_id
            [terms] => Array
                (
                    [0] => 10
                )

            [operator] => IN
        )
    [2] => Array
        (
            [taxonomy] => services
            [field] => term_id
            [terms] => Array
                (
                    [0] => 6
                )

            [operator] => IN
        )
    [3] => Array
        (
            [taxonomy] => services
            [field] => term_id
            [terms] => Array
                (
                    [0] => 9
                )

            [operator] => IN
        )

    [4] => Array
        (
            [taxonomy] => awards
            [field] => term_id
            [terms] => Array
                (
                    [0] => 13
                )

            [operator] => IN
        )
    [5] => Array
        (
            [taxonomy] => awards
            [field] => term_id
            [terms] => Array
                (
                    [0] => 12
                )

            [operator] => IN
        )

)

.

As I discovered while working through my recent advanced taxonomy query question, How to pass URL parameters for advanced taxonomy queries with multiple terms for one custom taxonomy WP will do just that (at least under 3.1.4) if you pass the query through the URL.

share|improve this answer

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.