Make WordPress Core

Opened 2 years ago

Closed 2 years ago

#62041 closed defect (bug) (duplicate)

WP_User_Query with compare_key always get 0 user

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

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
2 years ago

  • Resolution → duplicate
  • Status new → closed

Duplicate of #49284.

same issuu before 5 ysers.

#2 @fantasyworld
2 years ago

  • Resolution duplicate
  • Status closed → reopened

#3 @hellofromTonya
2 years ago

  • Milestone Awaiting Review
  • Resolution → duplicate
  • Status reopened → closed
  • Version 6.6.2 → 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.