admin管理员组

文章数量:1395752

I've been going at this issue for hours now, and please save me before i go crazy.....

I'm trying to use Wordpress' WP_User_Query to retrieve users from db

I've been feeding it with arguments like this, and it works fine...:

function getSomeUsers ($year, $month, $day)
{
    $arg =  [

        'meta_key'      => 'some_meta_key', 
        'meta_value'    => 'some_meta_value',

        'date_query'    => [
            [   
                'year'  => $year,
                'month' => $month,
                'day'   => $day
            ]
        ] 
     ];

    $user_query = new WP_User_Query( $arg );

    return $user_query;
}

But when I try to use a meta_query instead, it returns all users in db, and not just the ones that matches the query....

function getSomeUsers ()
{ 
    $arg =  [
            'meta_query' => [
                [
                    'key'     => 'some_meta_key',
                    'value'   => 'some_meta_value',
                    'compare' => '='
                ]
            ]
        ] ;

    $user_query = new WP_User_Query( $arg );

    return $user_query;
}

Anybody knows what's wrong?

edit: I've tried the meta query on another simple wordpress site, and it works there, so there must be something else going on, meddling with the query..

I've been doing some digging with the Query Monitor plugin... I thought it might have been because I've also been meddling with pre_user_query, but I can see that I don't have any actions attached to that hook... so nvm that...

but i can see the query being called. This is what it looks like, and as suspected the meta keys in the meta query is not being included:

SELECT DISTINCT SQL_CALC_FOUND_ROWS wp_users.*
FROM wp_users
LEFT JOIN wp_usermeta
ON (wp_users.ID = wp_usermeta.user_id
AND wp_usermeta.meta_key = 'ur_user_status' )
LEFT JOIN wp_usermeta AS mt1
ON ( wp_users.ID = mt1.user_id )
LEFT JOIN wp_usermeta AS mt2
ON ( wp_users.ID = mt2.user_id )
WHERE 1=1
AND ( ( ( wp_usermeta.user_id IS NULL
OR ( mt1.meta_key = 'ur_user_status'
AND mt1.meta_value = '1' ) )
AND ( mt2.meta_key = 'wp_3_capabilities' ) ) )
ORDER BY user_login AS

It includes some weird user metadata that seems to be added by the User Registration plugin.

I deactivated the plugin, and now the meta query works.... huh

I've been going at this issue for hours now, and please save me before i go crazy.....

I'm trying to use Wordpress' WP_User_Query to retrieve users from db

I've been feeding it with arguments like this, and it works fine...:

function getSomeUsers ($year, $month, $day)
{
    $arg =  [

        'meta_key'      => 'some_meta_key', 
        'meta_value'    => 'some_meta_value',

        'date_query'    => [
            [   
                'year'  => $year,
                'month' => $month,
                'day'   => $day
            ]
        ] 
     ];

    $user_query = new WP_User_Query( $arg );

    return $user_query;
}

But when I try to use a meta_query instead, it returns all users in db, and not just the ones that matches the query....

function getSomeUsers ()
{ 
    $arg =  [
            'meta_query' => [
                [
                    'key'     => 'some_meta_key',
                    'value'   => 'some_meta_value',
                    'compare' => '='
                ]
            ]
        ] ;

    $user_query = new WP_User_Query( $arg );

    return $user_query;
}

Anybody knows what's wrong?

edit: I've tried the meta query on another simple wordpress site, and it works there, so there must be something else going on, meddling with the query..

I've been doing some digging with the Query Monitor plugin... I thought it might have been because I've also been meddling with pre_user_query, but I can see that I don't have any actions attached to that hook... so nvm that...

but i can see the query being called. This is what it looks like, and as suspected the meta keys in the meta query is not being included:

SELECT DISTINCT SQL_CALC_FOUND_ROWS wp_users.*
FROM wp_users
LEFT JOIN wp_usermeta
ON (wp_users.ID = wp_usermeta.user_id
AND wp_usermeta.meta_key = 'ur_user_status' )
LEFT JOIN wp_usermeta AS mt1
ON ( wp_users.ID = mt1.user_id )
LEFT JOIN wp_usermeta AS mt2
ON ( wp_users.ID = mt2.user_id )
WHERE 1=1
AND ( ( ( wp_usermeta.user_id IS NULL
OR ( mt1.meta_key = 'ur_user_status'
AND mt1.meta_value = '1' ) )
AND ( mt2.meta_key = 'wp_3_capabilities' ) ) )
ORDER BY user_login AS

It includes some weird user metadata that seems to be added by the User Registration plugin.

I deactivated the plugin, and now the meta query works.... huh

Share Improve this question edited Mar 3, 2020 at 15:00 Mark Pedersen asked Mar 1, 2020 at 18:38 Mark PedersenMark Pedersen 113 bronze badges 4
  • Check this out: wordpress.stackexchange/questions/166723/… – Siddhesh Shirodkar Commented Mar 2, 2020 at 4:35
  • already tried that one :/ literally copied it and changed the keys and values to something i know is in my db..... it still returns every user in the db..... – Mark Pedersen Commented Mar 2, 2020 at 17:26
  • i'm running a multisite.... can that have anything to do with it? – Mark Pedersen Commented Mar 2, 2020 at 17:36
  • i have suspicion that it might be because i've been messing around with pre_user_query :/ i've used add_action( 'pre_user_query', 'add_custom_queries' ) to add some extra queries in order to order by meta data, but i've also used remove_action( 'pre_user_query', 'add_custom_queries' ) to undo the action after making the WP_User_Query( $arg ) call...... i might still have some actions hooked to pre_user_query..... anybody have a suggestion how to find out which actions are fired on pre_user_query ? – Mark Pedersen Commented Mar 3, 2020 at 11:46
Add a comment  | 

2 Answers 2

Reset to default 1

I've tried the meta query on another simple wordpress site, and it works there, so there must be something else going on, meddling with the query..

I've been doing some digging with the Query Monitor plugin... I thought it might have been because I've also been meddling with pre_user_query, but I can see that I don't have any actions attached to that hook... so nvm that...

but i can see the query being called. This is what it looks like, and as suspected the meta keys in the meta query is not being included:

SELECT DISTINCT SQL_CALC_FOUND_ROWS wp_users.*
FROM wp_users
LEFT JOIN wp_usermeta
ON (wp_users.ID = wp_usermeta.user_id
AND wp_usermeta.meta_key = 'ur_user_status' )
LEFT JOIN wp_usermeta AS mt1
ON ( wp_users.ID = mt1.user_id )
LEFT JOIN wp_usermeta AS mt2
ON ( wp_users.ID = mt2.user_id )
WHERE 1=1
AND ( ( ( wp_usermeta.user_id IS NULL
OR ( mt1.meta_key = 'ur_user_status'
AND mt1.meta_value = '1' ) )
AND ( mt2.meta_key = 'wp_3_capabilities' ) ) )
ORDER BY user_login AS

It includes some weird user metadata that seems to be added by the User Registration plugin.

I deactivated the plugin, and now the meta query works.... huh

Here is the reference code:

$args = array (
'role' => 'reporter',
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*'.esc_attr( $search_term ).'*',
'meta_query' => array(
    'relation' => 'OR',
    array(
        'key'     => 'first_name',
        'value'   => $search_term,
        'compare' => 'LIKE'
    ),
    array(
        'key'     => 'last_name',
        'value'   => $search_term,
        'compare' => 'LIKE'
    ),
    array(
        'key' => 'description',
        'value' => $search_term ,
        'compare' => 'LIKE'
     )
   )
);

 // Create the WP_User_Query object
 $wp_user_query = new WP_User_Query($args);

本文标签: