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.

  • It looks OK, so query itself is not the problem. Are you sure that these conditions are correct? – Krzysiek Dróżdż Sep 24 '18 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'. – proteuscanvas Sep 24 '18 at 6:49
  • the query you use won't work because meta_query only accept post meta – Tung Du Sep 24 '18 at 6:54
  • What should I be using to query the User Meta? – proteuscanvas Sep 24 '18 at 6:57
  • @TungDu of course you can use meta_query with WP_User_Query - just take a look at Codex ;) – Krzysiek Dróżdż Sep 24 '18 at 6:58
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.

  • Thanks, that was it. Also thanks for the explanation. – proteuscanvas Sep 24 '18 at 22:42

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.