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
add_action( 'pre_user_query', 'add_custom_queries' )
to add some extra queries in order to order by meta data, but i've also usedremove_action( 'pre_user_query', 'add_custom_queries' )
to undo the action after making theWP_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 ?