Advanced Tax Query

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
            )

    )

Related posts

Leave a Reply

2 comments

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