Unexpected results when running WP_User_Query with role and meta_query
|Reported by:||layotte||Owned by:|
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").
$args = array( 'role' => 'Author', 'number' => 100, 'offset' => 0, 'meta_query' => array( 'relation' => 'OR', array( 'key' => '_my_key', 'compare' => 'NOT EXISTS', ), array( 'key' => '_my_key', 'value' => 'off', 'compare' => 'NOT LIKE', ), ), ); $users = new WP_User_Query( $args );
Any author where _my_key either does not exist or if it does exists is set to "off"
All authors and/or all users with _my_key set.
The above generates this SQL:
SELECT DISTINCT SQL_CALC_FOUND_ROWS wp_users.* 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 depth.
I tested this in WP3.5 and Trunk
Change History (9)
comment:5 SergeyBiryukov — 11 months ago
- Summary changed from Unexepected results when running WP_User_Query with role and meta_query to Unexpected results when running WP_User_Query with role and meta_query