0

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.

6
  • It looks OK, so query itself is not the problem. Are you sure that these conditions are correct? Commented Sep 24, 2018 at 6:47
  • The only thing that I'm not sure about is the 'meta_key' vs 'key'. I am using ACF for 'organisation' and UserMeta for '$campaign_type'. Commented Sep 24, 2018 at 6:49
  • the query you use won't work because meta_query only accept post meta
    – Tung Du
    Commented Sep 24, 2018 at 6:54
  • What should I be using to query the User Meta? Commented Sep 24, 2018 at 6:57
  • @TungDu of course you can use meta_query with WP_User_Query - just take a look at Codex ;) Commented Sep 24, 2018 at 6:58

2 Answers 2

0

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.

1
  • Thanks, that was it. Also thanks for the explanation. Commented Sep 24, 2018 at 22:42
0
$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 );
1
  • 2
    Please edit your answer, and add an explanation: why could that solve the problem?
    – fuxia
    Commented Dec 14, 2020 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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