They are all convoluted ways of saying the same thing. Syntax 1 is the least efficient method, purely because it takes longer to type and uses unnecessary IN
clauses when a single clause will do.
All 3 syntaxes are asking WP for posts that have been assigned to both category 76 and 78
Note that if they are not the same as this:
// NOT the same
$wp_query = new WP_Query(array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array(76, 78),
'operator' => 'IN',
),
),
));
Why are there 3 ways of doing it though?
- syntax 1: is basically syntax 2 but more verbose. Part of this is that you’re trying to define conditions, and just like human language there’s more than one way to write the same thing
- syntax 2: if I were going to write this, this is close to how I’d write it, I’d probably add a post status of publish and add other parameters to try and constrain the search for performance
- syntax 3:
category__and
and the other top level parameters are there because they came first, and for backwards compatibility, as well as ease of use. If you look at the implementation ofWP_Query
you’ll see it gets converted into atax_query
clause. Note that these top level named conditions are less flexible and limiting compared to thetax_query
equivalents.- you’ll also find that for custom taxonomies this becomes even more limiting as the only top level custom taxonomy variable available is deprecated and the documentation directs you to use
tax_query
- you’ll also find that for custom taxonomies this becomes even more limiting as the only top level custom taxonomy variable available is deprecated and the documentation directs you to use