[wp-hackers] Limiting get_users() results by meta value

Mike Walsh mpwalsh8 at gmail.com
Fri Feb 1 16:13:35 UTC 2013


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).  I am stuck on how to
handle the wild card with the LIKE comparison.

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.  I can't seem to find what I
am searching for when I Google it, can anyone point me to an example of how
to do what I want to do?

Thanks,

Mike

-- 
Mike Walsh - mpwalsh8 at gmail.com


More information about the wp-hackers mailing list