[wp-trac] [WordPress Trac] #23849: Unexepected results when running WP_User_Query with role and meta_query

WordPress Trac noreply at wordpress.org
Fri Mar 22 19:48:50 UTC 2013

#23849: Unexepected results when running WP_User_Query with role and meta_query
 Reporter:  layotte          |       Type:  defect (bug)
   Status:  new              |   Priority:  normal
Milestone:  Awaiting Review  |  Component:  Query
  Version:                   |   Severity:  normal
 Keywords:  dev-feedback     |
 There appears to be a couple of bugs in WP_User_Query when doing a role
 query and adding additional meta_query options (especially when relation
 is set to "OR").

 This code:
 $args = array(
         'role'          => 'Author',
         'number'        => 100,
         'offset'        => 0,
         'meta_query' => array(
                 'relation' => 'OR',
                         'key'           => '_my_key',
                         'compare'       => 'NOT EXISTS',
                         'key'           => '_my_key',
                         'value'         => 'off',
                         'compare'       => 'NOT LIKE',
 $users = new WP_User_Query( $args );

 Expected results:
 Any author where _my_key either does not exist or if it does exists is set
 to "off"

 Returned results:
 All authors and/or all users with _my_key set.

 The above generates this SQL:
 FROM wp_users
 INNER JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_id
 INNER JOIN wp_usermeta AS mt1 ON (wp_users.ID = mt1.user_id)
 INNER JOIN wp_usermeta AS mt2 ON (wp_users.ID = mt2.user_id)
 WHERE 1=1
 AND (wp_usermeta.meta_key = '_pigeonpack_subscription'
         OR  (mt1.meta_key = '_pigeonpack_subscription' AND
 CAST(mt1.meta_value AS CHAR) NOT LIKE '%off%')
         OR  (mt2.meta_key = 'wp_capabilities' AND CAST(mt2.meta_value AS
 CHAR) LIKE '%\"Author\"%') )
 ORDER BY user_login ASC LIMIT 100

 This seems like a bug to me, the "Author" meta should not be modified by
 the relation => 'OR' argument and thus should not be included in the other
 meta query statement. Further, the NOT EXISTS isn't a true NOT EXISTS
 statement. In fact, I'm not even sure why the NOT EXISTS statement looks
 the way it does. I haven't had too much time to look into this any more in

 I tested this in WP3.5 and Trunk

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

More information about the wp-trac mailing list