WordPress.org

Make WordPress Core

Opened 6 years ago

Last modified 3 months ago

#23373 reopened enhancement

Limiting get_users() results by meta value with LIKE wildcards

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

Description (last modified by SergeyBiryukov)

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.

Attachments (1)

meta.php.patch (1.3 KB) - added by mpwalsh8 6 years ago.
Patch for meta.php to support this functionality

Download all attachments as: .zip

Change History (5)

@mpwalsh8
6 years ago

Patch for meta.php to support this functionality

#1 @SergeyBiryukov
6 years ago

  • Description modified (diff)
  • Version changed from trunk to 3.5

#2 @MikeHansenMe
4 years ago

  • Keywords needs-refresh added

#3 @iseulde
5 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

This ticket has not seen any activity in over *two* years, so I'm closing it as "wontfix".

The ticket may lack decisiveness, may have become irrelevant, or may not have gathered enough interest.

If you think this ticket does deserve some attention again, feel free to reopen.

For bugs, it would be great if you could provide updated steps to reproduce against the latest version of WordPress (5.0.2 at the time of writing). Remember images or a video can be superior to explain a problem. At the very least, quickly test again to make sure the bug still exists.

If it’s an enhancement or feature, some extra motivation may help.

Thank you for your contributions to WordPress! <3

#4 @JeffPaul
3 months ago

  • Keywords bulk-reopened added
  • Milestone set to Awaiting Review
  • Resolution wontfix deleted
  • Status changed from closed to reopened

A decision was made to reopen tickets that were closed in the bulk edit that this ticket was affected by. This ticket is being placed back into the Awaiting Review milestone so it can be individually evaluated and verified to determine if it is still relevant/valid or reproducible.

Note: See TracTickets for help on using tickets.