Opened 7 years ago
#41719 new defect (bug)
Malformed User Search results
Reported by: | navepilif | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 4.8 |
Component: | Users | Keywords: | |
Focuses: | Cc: |
Description
There is a logic bug with the User Search WP_User_Query. (Users > All Users > "Search Users" (in top-right corner)).
The SQL that is generated looks something like this:
SELECT SQL_CALC_FOUND_ROWS wp_users.ID FROM wp_users INNER JOIN wp_usermeta ON ( wp_users.ID = wp_usermeta.user_id ) WHERE ( 1=1 AND ( user_login LIKE '%Search Term%' OR user_url LIKE '%Search Term%' OR user_email LIKE '%Search Term%' OR user_nicename LIKE '%Search Term%' OR display_name LIKE '%Search Term%' ) ) ORDER BY user_login ASC LIMIT 0, 20;
However, such a query is not guaranteed to return unique user IDs. It just so "happens" to work right now. The more correct query adds the DISTINCT keyword:
SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_users.ID ...
Modifying the query in certain situations results in duplicate user IDs being returned. One example, of many such modifications that could be made, can be seen in the Better User Search plugin on line https://github.com/wp-plugins/better-user-search/blob/master/better-user-search.php#L200.
As is, this is currently breaking pagination and sometimes displaying redundant users as you flip through the pages. (See attached screenshot. My pagination is set to 20, and I have 100 matching users on my system).
Broken pagination and user results