Meta_query imbriquée avec plusieurs clés de relation
3 réponses
- votes
-
- 2015-03-19
La question étaitpour Wordpress 3.0,maisjuste au cas où quelqu'un aurait lamême questionpour une versionplus récente,de Wordpress Codex:
"Àpartir de la version 4.1,les clausesmeta_querypeuvent êtreimbriquées afin de construire des requêtes complexes."
https://developer.wordpress.org/reference/classes/wp_query/# custom-field-post-meta-parameters Donc,cette requête devraitfonctionner sur la version actuelle de Wordpress.
The question was for Wordpress 3.0, but just in case someone has the same question for a more recent version, from Wordpress Codex:
"Starting with version 4.1, meta_query clauses can be nested in order to construct complex queries."
https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters So, that query should work on the current Wordpress version.
-
- 2013-01-18
Cela sembleimpossible. S'il vousplaît,quelqu'unme corrige sije metrompe.
Leparamètre
meta_query
seraen faittransforméen un objetWP_Meta_Query
,et la vérification de larelation
n'irapasplus loin danswp-includes/meta.php
,et seproduitjuste unefois auniveau supérieur:if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) { $this->relation = 'OR'; } else { $this->relation = 'AND'; }
Une solutionpossiblepour celaest de créer votrepropre JOINpour cette requête.
$query = new WP_Query( array( ... 'my_meta_query' => true, 'suppress_filters' => false ) ); add_filter( 'posts_join', 'my_meta_query_posts_join', 10, 2 ); function my_meta_query_posts_join( $join, $query ) { if ( empty( $query->query_vars['my_meta_query'] ) ) return $join; global $wpdb; $new_join = " INNER JOIN {$wpdb->postmeta} pm1 ON 1=1 AND pm1.post_id = {$wpdb->posts}.ID AND pm1.meta_key = '_some_meta_key' AND pm1.meta_value = 'some_value' "; return $join . ' ' . $new_join; }
Et si vous avezbesoin de vérificationset de règles supplémentaires,vouspouvez également utiliser lefiltre
posts_where
.That seems to be impossible. Please someone correct me if I'm wrong.
The
meta_query
parameter will actually be transformed into aWP_Meta_Query
object, and therelation
verification won't go deeper inwp-includes/meta.php
, and occurs just once in the top level:if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) { $this->relation = 'OR'; } else { $this->relation = 'AND'; }
A possible solution for this is to build your own JOIN for this query.
$query = new WP_Query( array( ... 'my_meta_query' => true, 'suppress_filters' => false ) ); add_filter( 'posts_join', 'my_meta_query_posts_join', 10, 2 ); function my_meta_query_posts_join( $join, $query ) { if ( empty( $query->query_vars['my_meta_query'] ) ) return $join; global $wpdb; $new_join = " INNER JOIN {$wpdb->postmeta} pm1 ON 1=1 AND pm1.post_id = {$wpdb->posts}.ID AND pm1.meta_key = '_some_meta_key' AND pm1.meta_value = 'some_value' "; return $join . ' ' . $new_join; }
And if you need further verifications and rules, you can also use the
posts_where
filter.-
Semble être lamême approche que @scribu recommande sur Core Trac: https://core.trac.wordpress.org/ticket/20312Seems to be the same approach @scribu recommends on Core Trac: https://core.trac.wordpress.org/ticket/20312
- 3
- 2014-05-29
- Andrew Odri
-
@AndrewOdri c'estpareilmais cen'estpas le cas=)@AndrewOdri it's the same but it's not =)
- 0
- 2015-10-06
- vmassuchetto
-
- 2018-08-07
En attendant,celaestpossible,voir la documentation avec desexempleset desexplications:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
et un autreexemple https://wordpress.org/support/topic/wp_query-with-multiple-meta_query/#post-9410992
'meta_query' => array( 'relation' => 'OR', array( 'relation' => 'AND', array( 'key' => '_price', 'value' => 1, 'compare' => '>=', 'type' => 'DECIMAL', ), array( 'key' => '_price', 'value' => 3000, 'compare' => '<=', 'type' => 'DECIMAL', ), ), array( 'relation' => 'AND', array( 'key' => '_price', 'value' => 3001, 'compare' => '>=', 'type' => 'DECIMAL', ), array( 'key' => '_price', 'value' => 6000, //fixed <= to => 'compare' => '<=', 'type' => 'DECIMAL', ), ) ),
Meanwhile this is possible, see documentation with example and explanation:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
and another example https://wordpress.org/support/topic/wp_query-with-multiple-meta_query/#post-9410992
'meta_query' => array( 'relation' => 'OR', array( 'relation' => 'AND', array( 'key' => '_price', 'value' => 1, 'compare' => '>=', 'type' => 'DECIMAL', ), array( 'key' => '_price', 'value' => 3000, 'compare' => '<=', 'type' => 'DECIMAL', ), ), array( 'relation' => 'AND', array( 'key' => '_price', 'value' => 3001, 'compare' => '>=', 'type' => 'DECIMAL', ), array( 'key' => '_price', 'value' => 6000, //fixed <= to => 'compare' => '<=', 'type' => 'DECIMAL', ), ) ),
Je suis curieux de savoir si Wordpressest capable d'exécuter des
meta_query
imbriqués,chacun ayant des clés de relation différentes? Depuis Wordpress 3.0,tax_query
est capable d'exécuter cettefonction; Jeme demande si cela a un équivalent avecmeta_query
.Références: