[wp-trac] [WordPress Trac] #25421: `get_users`, role parameter conflict with multiple meta_query and OR relation

WordPress Trac noreply at wordpress.org
Thu Sep 26 15:34:31 UTC 2013


#25421: `get_users`, role parameter conflict with multiple meta_query and OR
relation
--------------------------+-----------------------------
 Reporter:  takien        |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Users         |    Version:  3.6.1
 Severity:  normal        |   Keywords:  needs-patch
--------------------------+-----------------------------
 affected function/class: `WP_User_Query` or `get_users`

 role parameter when it combined with meta_query, it will produce
 unexpected result.

 '''PROBLEM/EXAMPLE''': it's impossible to make query user with role
 'editor' which has last_name 'alex' OR first_name 'alex'


 {{{
 $args = Array(
    'role'  => 'editor',

    'meta_query' => Array(
        'relation' => 'OR',
        array(
         'key'     => 'first_name',
         'value'   => 'alex',
         'compare' => 'LIKE'),
        array(
          'key'     => 'last_name',
          'value'   => 'alex',
          'compare' => 'LIKE')
     ));
 }}}

 query debug:

 {{{
 WHERE 1=1 AND ( (wp_usermeta.meta_key = 'first_name' AND
 CAST(wp_usermeta.meta_value AS CHAR) LIKE '%alex%')
 OR  (mt1.meta_key = 'last_name' AND CAST(mt1.meta_value AS CHAR) LIKE
 '%alex%')
 OR  (mt2.meta_key = 'wp_capabilities' AND CAST(mt2.meta_value AS CHAR)
 LIKE '%\"editor\"%') )
 }}}
 it will simply return all 'editor'.


 '''SOLUTION''': role should use AND relation independently regardless the
 custom relation.

--
Ticket URL: <http://core.trac.wordpress.org/ticket/25421>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list