WP_User_Query with 2 sets of conditions 'AND'
-
22-04-2021 - |
Question
I would like to write some arguments for my query and both need to be true. I am having trouble with the Syntax.
'meta_key' => $campaign_type,
'meta_value' => $Campaign_ID,
AND
'meta_key' => 'organisation',
'meta_value' => $userOrg,
Below is my query code:
$args = array (
'meta_query' => array(
'relation' => 'AND',
array(
'meta_key' => $campaign_type,
'meta_value' => $Campaign_ID,
'compare' => '='
),
array(
'meta_key' => 'organisation',
'meta_value' => $userOrg,
'compare' => '='
)
));
Thank you.
Solution
You have an example here: https://codex.wordpress.org/Class_Reference/WP_User_Query#Custom_Field_Parameters
Problem with your query is that you use wrong keys. It should be:
$args = array (
'meta_query' => array(
'relation' => 'AND',
array(
'key' => $campaign_type,
'value' => $Campaign_ID,
'compare' => '='
),
array(
'key' => 'organisation',
'value' => $userOrg,
'compare' => '='
)
));
Longer versions (meta_key
, meta_value
, meta_compare
) are used, when you don't use meta_query
but put them directly in the query.
OTHER TIPS
$args = array(
'meta_key' => 'last_name',
's' => $_GET['search_text'],
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'name_organization', //ACF custom field
'value' => $text,
'compare' => 'LIKE'
),
array(
'key' => 'first_name', //User name
'value' => $text,
'compare' => 'LIKE'
),
)
);
$user_query = new WP_User_Query( $args );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange