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
)
)
Try
[operator] => AND
in both the sub-arrays. I think that will work from my reading ofWP_Tax_Query::get_sql()
.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.)
.
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.