[wp-trac] [WordPress Trac] #32592: Duplicate user results when using nested meta queries with inner OR relation

WordPress Trac noreply at wordpress.org
Mon Jun 8 19:49:00 UTC 2015


#32592: Duplicate user results when using nested meta queries with inner OR
relation
--------------------------+-----------------------------
 Reporter:  maxxsnake     |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Users         |    Version:  4.2.2
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 When using nested meta queries as recently introduced
 (https://make.wordpress.org/core/2014/10/20/update-on-query-improvements-
 in-4-1/), the query returns duplicate users when multiple matches are
 found on the inner "OR" relation queries, as "DISTINCT" is omitted from
 the generated SQL select.

 {{{
 <?php

 $args = array(
         'count_total' => true,
         'meta_query' => array(
                 'relation' => 'AND',
                 array(
                         'first_name_clause' => array(
                                 'key' => 'first_name',
                                 'compare' => 'EXISTS',
                         ),
                         'last_name_clause' => array(
                                 'key' => 'last_name',
                                 'compare' => 'EXISTS',
                         ),
                 ),
                 array(
                         'relation' => 'OR', // Inner OR, fails to add
 DISTINCT to query_fields
                         array(
                                 'compare' => 'LIKE',
                                 'key' => 'first_name',
                                 'value' => 'jo',
                         ),
                         array(
                                 'compare' => 'LIKE',
                                 'key' => 'last_name',
                                 'value' => 'jo',
                         ),
                         array(
                                 'compare' => 'LIKE',
                                 'key' => 'name_custom_meta_key',
                                 'value' => 'jo',
                         ),
                 ),
         ),
         'number' => 2,
         'orderby' => 'last_name_clause first_name_clause',
         'role' => 'roster_agent',
 );
 $query = new WP_User_Query( $args );

 ?>
 }}}

 In user.php, starting on line 677, I noticed that DISTINCT is only added
 when the relation is set to OR:


 {{{
 <?php

 if ( 'OR' == $this->meta_query->relation ) {
         $this->query_fields = 'DISTINCT ' . $this->query_fields;
 }

 ?>
 }}}

 The relation appears to be set based on the root-level meta_query key
 only, as is set during instantiation of the WP_Meta_Query class in
 meta.php starting on line 979.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/32592>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list