0

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

4
  • 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 Mar 2 '20 at 17:26
  • i'm running a multisite.... can that have anything to do with it? – Mark Pedersen Mar 2 '20 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 Mar 3 '20 at 11:46
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

0

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);
1
  • thanks for the answer man ^_^ i copied this over and messed around with it, but it seems like the meta_query does absolutely nothing..... role and search will have an influence on the results returned by the query. When i cut those out, i still have the issue of every user in db being returned.... search seems to be working, but it returns users where user_login, first_name, last_name, or email adress corresponds.... not just first and last name like the meta_query states :/ – Mark Pedersen Mar 2 '20 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.