Make WordPress Core

#62041 closed defect (bug) (duplicate)

WP_User_Query with compare_key always get 0 user

Reported by: fantasyworld's profile fantasyworld Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.1
Component: Query Keywords:
Focuses: Cc:

Description

The user query alway get 0 user.

$args = [
    'meta_query' => [
        'relation' => 'OR',
        [
            'key' => 'nickname',
            'value' => 'test',
            'compare' => '!=',
        ],
        [
            'key' => 'nickname',
            'compare_key' => 'NOT EXISTS',
        ],
    ]
];
$user_search = new WP_User_Query($args);

It is because the real query sql try to user postmeta table in user query.

SELECT DISTINCT SQL_CALC_FOUND_ROWS wp_users.ID
    FROM wp_users INNER JOIN wp_usermeta ON ( wp_users.ID = wp_usermeta.user_id )  INNER JOIN wp_usermeta AS mt1 ON ( wp_users.ID = mt1.user_id )
    WHERE 1=1 AND (
        ( wp_usermeta.meta_key = 'nickname' AND wp_usermeta.meta_value != 'test' ) 
        OR
        NOT EXISTS (SELECT 1 FROM wp_postmeta mt2 WHERE mt2.post_ID = mt1.post_ID AND mt2.meta_key = 'nickname' LIMIT 1)
    )
ORDER BY user_login ASC

Change History (3)

#1 @fantasyworld
19 months ago

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #49284.

same issuu before 5 ysers.

#2 @fantasyworld
19 months ago

  • Resolution duplicate deleted
  • Status changed from closed to reopened

#3 @hellofromTonya
19 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from reopened to closed
  • Version changed from 6.6.2 to 5.1

Hello @fantasyworld,

Welcome back to WordPress Core Trac.

Yes, you were previously, as this is already reported in #49284. That specific ticket describes passing the 'meta_query' args to get_users(). In get_users(), it'll handle the instantiation of WP_User_Query( $args ), which is what you describe.

Reclosing this ticket as duplicate to move the discussion and resolution to one ticket, i.e. #49284. Inviting you to share your findings and join the effort to resolve it in that ticket. Thanks.

Also updating the Version. No changes occurred in 6.6 or 6.6.2 to affect this code. Last the change to compare_key in WP_Meta_Query was during 5.1 via #43346 / [46188]. Though I suspect its from an early version, I'll mark Version as 5.1 to denote what is currently known.

Note: See TracTickets for help on using tickets.