[wp-trac] [WordPress Trac] #32372: WP_User_Query fails on search_columns AND relation

WordPress Trac noreply at wordpress.org
Wed May 13 15:18:16 UTC 2015


#32372: WP_User_Query fails on search_columns AND relation
--------------------------+-----------------------------
 Reporter:  tareq1988     |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Users         |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 I am searching for users with this query

 {{{
 <?php
 $args = array(
     'search' => '*' . $search_query . '*',
     'search_columns' => array(
         'user_login',
         'user_nicename',
     ),
     'meta_query' => array(
         'relation' => 'OR',
         array(
             'key'     => 'first_name',
             'value'   => $search_query,
             'compare' => 'LIKE'
         ),
         array(
             'key'     => 'last_name',
             'value'   => $search_query,
             'compare' => 'LIKE'
         ),
         array(
             'key'     => 'description',
             'value'   => $search_query,
             'compare' => 'LIKE'
         ),
     )
 );

 $meta_users = new WP_User_Query( $args );
 }}}

 **Example Search**: John
 **User on DB**:
  - first_name: John
  - last_name: Doe
  - user_login: admin
  - user_nicename: admin

 **Generated Query:**

 {{{
 SELECT DISTINCT SQL_CALC_FOUND_ROWS wp_users.*
 FROM wp_users
 INNER JOIN wp_usermeta ON ( wp_users.ID = wp_usermeta.user_id )
 WHERE 1=1
 AND (
      (wp_usermeta.meta_key = 'first_name' AND CAST(wp_usermeta.meta_value
 AS CHAR) LIKE '%john%' )
   OR ( wp_usermeta.meta_key = 'last_name' AND CAST(wp_usermeta.meta_value
 AS CHAR) LIKE '%john%' )
   OR ( wp_usermeta.meta_key = 'description' AND
 CAST(wp_usermeta.meta_value AS CHAR) LIKE '%john%' )
 )
 AND (user_login LIKE '%john%' OR user_nicename LIKE '%john%')
 ORDER BY user_login ASC
 LIMIT 20
 }}}

 The query fails because `user_columns` are joining with `AND`. Having `OR`
 in query would return the correct result. But currently there is no way to
 define `OR` because its hardcoded on the
 [https://core.trac.wordpress.org/browser/trunk/src/wp-
 includes/user.php#L922 get_search_sql] function. We should be able to
 define `OR/AND` by ourselves and if not defined, it should fallback to
 `AND`.

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


More information about the wp-trac mailing list