Relation multiple pour plusieurs tax_query dans WP_Query
2 réponses
- votes
-
- 2013-04-18
Celapeut êtrefaiten utilisant leterm_taxonomy_idplutôt que le slug,quiignoreraeffectivement lataxonomie spécifiéeet examinera simplement le champ uniqueterm_taxonomy_id. Cela vouspermettra de créerefficacement une relationmixte. Vous voudriez utiliser une relationglobale de ANDet mettretous lestermes qui devraient être liés OU dans un élément à l'aide de l'opérateur IN. Vous devrez d'abordmapper lestermes souhaités avec leursterm_taxonomy_ids.
$taxes = array( 'taxonomy1', 'taxonomy2', 'taxonomy3', 'taxonomy4' ); foreach ( $taxes as $tax ) { $terms = get_terms( $tax ); foreach ( $terms as $term ) $tax_map[$tax][$term->slug] = $term->term_taxonomy_id; } $args['tax_query'] => array( 'relation' => 'AND', array( 'taxonomy' => 'taxonomy1', 'field' => 'term_taxonomy_id', 'terms' => array( $tax_map['taxonomy1'][$slug] ) 'operator' => 'IN', ), array( 'taxonomy' => 'taxonomy3', 'field' => 'term_taxonomy_id', 'terms' => array( $tax_map['taxonomy3'][$slug] ), 'operator' => 'IN', ), array( 'taxonomy' => 'taxonomy4', // gets ignored 'field' => 'term_taxonomy_id', 'terms' => array( $tax_map['taxonomy4'][$slug], $tax_map['taxonomy2'][$slug] ), 'operator' => 'IN', ), );
Notez qu'avant la version 3.5,vous devez également spécifier
'include_children' => false
. Voir ceticket Tracpouren savoirplus: https://core.trac.wordpress.org/ticket/21228This can be done by using the term_taxonomy_id rather than the slug, which will effectively ignore whichever taxonomy is specified and just look at the unique term_taxonomy_id field. This will allow you to effectively be able to do a mixed relationship. You'd want to use an overall relation of AND and put all the terms that should be related OR in one item using the IN operator. You will need to map the terms desired to their term_taxonomy_ids first.
$taxes = array( 'taxonomy1', 'taxonomy2', 'taxonomy3', 'taxonomy4' ); foreach ( $taxes as $tax ) { $terms = get_terms( $tax ); foreach ( $terms as $term ) $tax_map[$tax][$term->slug] = $term->term_taxonomy_id; } $args['tax_query'] => array( 'relation' => 'AND', array( 'taxonomy' => 'taxonomy1', 'field' => 'term_taxonomy_id', 'terms' => array( $tax_map['taxonomy1'][$slug] ) 'operator' => 'IN', ), array( 'taxonomy' => 'taxonomy3', 'field' => 'term_taxonomy_id', 'terms' => array( $tax_map['taxonomy3'][$slug] ), 'operator' => 'IN', ), array( 'taxonomy' => 'taxonomy4', // gets ignored 'field' => 'term_taxonomy_id', 'terms' => array( $tax_map['taxonomy4'][$slug], $tax_map['taxonomy2'][$slug] ), 'operator' => 'IN', ), );
Note that pre-3.5, you'd also need to specify
'include_children' => false
. See this Trac ticket for more: https://core.trac.wordpress.org/ticket/21228-
Aufait,excusez-moiet alertez-moi detouteerreur/inexactitude :) Tapertout cela d'unemain avec unbébé dans l'autre.By the way, please excuse and alert me to any mistakes/inaccuracies :) Typing all this one-handed with a baby in the other.
- 6
- 2013-04-18
- helenhousandi
-
D'où vient `$ slug` ...?Where does `$slug` come from...?
- 1
- 2013-04-18
- vancoder
-
Juste unespace réservé dans le code.J'imagine que c'était $term1etc. dans la questioninitiale.Just a placeholder in the code. I guess it was $term1 etc. in the original question.
- 1
- 2013-04-18
- helenhousandi
-
Vous avez raison!Cetteméthode semblefonctionner.Je vais letesteret revenir voterpour cette réponse si celafonctionne.MerciYou are right! This method seem to work. I will give it test, and come back to vote for this answer if it is working. Thanks
- 0
- 2013-04-19
- ron_dev
-
Il semble que la ``taxonomie '=>'taxonomie4 ',//estignorée`ne peutpas êtreignorée.Sije mets dutexte aléatoire dans ce champ,aucun résultatn'a ététrouvé.Cen'est que lorsquej'ai attribué un vrainom detaxonomie que celame donne le résultat.Uneidéepourquoi?It seem the `'taxonomy' => 'taxonomy4', // gets ignored` cannot be ignored. If i put random text in this field, no result was found. Only when I assigned real taxonomy name, it give me the result. Any idea why?
- 0
- 2013-04-19
- ron_dev
-
Il vérifieprobablement si lataxeexiste quelquepart le long de la ligne.It probably checks to see if the tax exists somewhere along the line.
- 0
- 2013-04-19
- helenhousandi
-
Mais d'après ce quej'ai vu dans https://core.trac.wordpress.org/ticket/21228et https://core.trac.wordpress.org/changeset/21552,ilne devraitpas vérifier lataxonomie lorsque 'term_taxonomy_id'existe,non?But from what I saw in https://core.trac.wordpress.org/ticket/21228 and https://core.trac.wordpress.org/changeset/21552 , it shouldn't check for the taxonomy when 'term_taxonomy_id' exist, right?
- 0
- 2013-04-20
- ron_dev
-
Après avoiressayéplusieursfois,celane fonctionnetoujourspas,je pense que cetteméthodene fonctionnepas ... après avoir vérifié la classe WP_TAX_QUERY à https://core.trac.wordpress.org/browser/trunk/wp-includes/taxonomy.php?rev=21552,j'ai découvert qu'il étaitencorenécessaire de vérifier lenom de lataxonomie avant depouvoir utiliser `term_taxonomy_id`pour ceporpuse (corrigez-moi sije metrompe),donc lenom de lataxonomie doitexister dans letableautax_query.Par conséquent,cetteméthodene peutpas résoudre leproblème.Une autre suggestionpour résoudre ceproblème?After I tried many times, it still doesn't work, I think this method is not working...after i checked on the WP_TAX_QUERY Class at https://core.trac.wordpress.org/browser/trunk/wp-includes/taxonomy.php?rev=21552, I found out it's till required to check on the taxonomy name before you can use `term_taxonomy_id` for this porpuse (correct me if i am wrong), so the taxonomy name must exist in the tax_query array. Therefor, this method can't solve the problem. Any other suggest to solve this problem?
- 0
- 2013-04-23
- ron_dev
-
D'accord,j'aifinalement écrit ceci (et l'aipublié): http://10up.com/blog/wordpress-mixed-relationship-taxonomy-queries/-en gros,oui,vous devez utiliser unnom detaxonomie valide,mais lereste se dresse.Okay, I finally wrote this up (and published it): http://10up.com/blog/wordpress-mixed-relationship-taxonomy-queries/ - basically, yes, you do have to use a valid taxonomy name, but the rest stands.
- 0
- 2013-12-16
- helenhousandi
-
-
Unebien meilleure suggestion.Commeil a étépublié quelquetemps après la réponse ci-dessus.Much better suggestion. As it was released some time after above answer.
- 0
- 2020-02-12
- FooBar
-
Je souhaite utiliser la classe
WP_Query()
pourfiltrer certains demesmessages. Leproblème auquelje suis confrontémaintenantest letraitement de la requête detaxonomie. Normalement,leWP_Query()
ne gère qu'une seule relationpourtax_query()
(AND ou OR),mais ce dontj'aibesoinest une utilisationmixte de ces relations sur letax_query()
,comment yparvenir?parexemple
Je sais que le code ci-dessusne fonctionnepas,dois-je utiliser lefiltre
WP_Query()
pour lefaire? Uneidée?