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 |2 Answers
Reset to default 1I'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);
本文标签:
版权声明:本文标题:meta query - meta_query in WP_User_Query not working - returns every user, not just users where key & value matches 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744700507a2620535.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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 ? – Mark Pedersen Commented Mar 3, 2020 at 11:46