WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 5 weeks ago

#23373 new enhancement

Limiting get_users() results by meta value with LIKE wildcards — at Initial Version

Reported by: mpwalsh8 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.5
Component: Users Keywords: has-patch needs-refresh
Focuses: Cc:

Description

I want to limit the results returned by get_users() to a subset of the users based on some criteria that will be stored in the User Meta table (e.g. all users whose first name begins with M).

Here arr the args I am passing to get_users():
(

[include] =>
[exclude] =>
[fields] => all_with_meta
[meta_key] => last_name
[meta_value] => M%
[meta_compare] => LIKE

)

This is the query the WordPress constructs for me:

[01-Feb-2013 16:01:08 UTC] WP_User_Query Object
(

[query_vars] => Array

(

[blog_id] => 1
[role] =>
[meta_key] => last_name
[meta_value] => M%
[meta_compare] => LIKE
[include] =>
[exclude] =>
[search] =>
[search_columns] => Array

(
)

[orderby] => login
[order] => ASC
[offset] =>
[number] =>
[count_total] =>
[fields] => all_with_meta
[who] =>

)

[results] => Array

(
)

[total_users] => 0
[query_fields] => wp_users.ID
[query_from] => FROM wp_users INNER JOIN wp_usermeta ON (wp_users.ID = wp_usermeta.user_id)
[query_where] => WHERE 1=1 AND ( (wp_usermeta.meta_key = 'last_name' AND CAST(wp_usermeta.meta_value AS CHAR) LIKE '%M
%%') )
[query_orderby] => ORDER BY user_login ASC
[query_limit] =>


As you can see, the LIKE clause is escaped and wrapped with SQL wildcard operators. I can't figure out how to prevent this from happening as what I want is "LIKE 'M%'" to be my actually query.


Andrew Nacin wp@… via lists.automattic.com
11:24 am (1 day ago) to wp-hackers

Currently, "LIKE" prepends and appends wildcards, as in %term%. We could add "%LIKE" and "LIKE%", though that would not help with the situation where you want LIKE taken literally, potentially without % wildcards at all. (Such as using _ as a single-character wildcard, or looking for case insensitive matching.) Probably the best way to add to this API would be to allow for a 'like_escape' => false; that would accept your value as provided. If you'd like to submit a ticket (and patch), this can definitely find its way into 3.6.

Nacin


I have prepared a patch to meta.php which allows escaped LIKE and NOT LIKE meta compares to work properly.

Change History (1)

@mpwalsh82 years ago

Patch for meta.php to support this functionality

Note: See TracTickets for help on using tickets.