Qual è la differenza tra la RELAZIONE "AND" e "OR" in TAX_QUERY?

31 lug 2013, 15:36:24
Visualizzazioni: 23.2K
Voti: 3

Perché se uso:

'relation' => 'AND'

oppure

'relation' => 'OR'

i risultati sono sempre gli stessi. Perché?

0
Tutte le risposte alla domanda 2
1

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

31 lug 2013 17:58:25
Commenti

Se ogni post che corrisponde a una delle query di tassonomia corrisponde anche a tutte le altre, non ci sarà alcuna differenza nei post restituiti tra relation impostato su OR e AND. Questo è banalmente vero nel caso in cui si abbia solo una query di tassonomia.

Stephen Harris Stephen Harris
31 lug 2013 18:02:04
0

Con AND i post restituiti devono avere entrambi i termini.

Con OR i post restituiti devono avere solo uno o l'altro termine.

Se restituiscono gli stessi risultati, probabilmente tutti i post che stai interrogando hanno entrambi i termini.

31 lug 2013 15:41:23