Make WordPress Core

Opened 7 years ago

#41719 new defect (bug)

Malformed User Search results

Reported by: navepilif's profile 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).

Attachments (2)

wp_bug_1.png (61.1 KB) - added by navepilif 7 years ago.
Broken pagination and user results
wp_bug_2.png (56.8 KB) - added by navepilif 7 years ago.
Broken pagination and user results (page 2)

Download all attachments as: .zip

Change History (2)

@navepilif
7 years ago

Broken pagination and user results

@navepilif
7 years ago

Broken pagination and user results (page 2)

Note: See TracTickets for help on using tickets.