Qual è la differenza tra la RELAZIONE "AND" e "OR" in TAX_QUERY?
Perché se uso:
'relation' => 'AND'
oppure
'relation' => 'OR'
i risultati sono sempre gli stessi. Perché?

tax_query
è un array bidimensionale, ogni sotto-array può essere considerato come una query tassonomica, tipicamente nella forma:
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob'
)
(ad esempio, post con il termine 'bob' come slug, nella tassonomia 'people').
Il fatto che tax_query
sia un array bidimensionale significa che puoi avere multiple query tassonomiche. In questo caso potresti voler restituire post che corrispondono a tutte le query tassonomiche (relation => 'AND'
) o post che corrispondono ad almeno una query tassonomica (relation => 'OR'
).
Come afferma il codex:
Questa struttura permette di interrogare più tassonomie utilizzando il parametro relation nel primo array (esterno) per descrivere la relazione booleana tra le query tassonomiche.
Ad esempio:
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'AND', //Deve soddisfare tutte le query tassonomiche
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => 'action'
),
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
restituisce tutti i post con il termine 'action' come slug nella tassonomia movie_genre
E che non hanno i termini actor
con ID 103
, 115
, 206
.
D'altra parte (nota il cambio di relation
):
$args = array(
'post_type' => 'post',
'tax_query' => array(
'relation' => 'OR', //Deve soddisfare almeno una query tassonomica
array(
'taxonomy' => 'movie_genre',
'field' => 'slug',
'terms' => 'action'
),
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => array( 103, 115, 206 ),
'operator' => 'NOT IN'
)
)
);
$query = new WP_Query( $args );
restituisce tutti i post che hanno il termine 'action' come slug nella tassonomia movie_genre
OPPURE che non hanno i termini actor
con ID 103
, 115
, 206
(o che soddisfano entrambe le condizioni).
