0

it's possible to show user if my logic like this it's better to give illustration

USER

I have two users with usermeta:
user A have usermeta country with value "USA" and univ with value "UCLA"
user B ONLY have usermeta country with value "USA"

POST

I also have two posts:
post 1 contain postmeta country with value "USA" 
post 2 ONLY have postmeta country with value "USA" and state with value "TX"

LOGIC

IF post have postmeta "COUNTRY" => "USA":
ECHO user A & user B
IF post have postmeta "COUNTRY" => "USA" AND have postmeta "UNIV" => "UCLA"
ONLY ECHO user A

so in post 1 show all user A & b but in post 2 only show user A because user A have state.

My code so far

get_users(array(
    'orderby' => 'display_name',
    'order' => 'asc',
    'meta_query' => array(
        'relation' => 'OR', // Optional, defaults to "AND"
        array(
            'key' => 'verifier_assign_country',
            'value' => $countryCode,
            'compare' => '='
        ),
        array(
            'relation' => 'AND',
            array(
                'key' => 'verifier_assign_univ',
                'value' => $univCode,
                'compare' => 'LIKE'
            )
        )
    )
), 'objects');

Thank you for help

1 Answer 1

0

you can create conditionnal arguments like that :

$user_args = [
    'orderby' => 'display_name',
    'order' => 'asc',
    'meta_query' => array(
        array(
            'key' => 'verifier_assign_country',
            'value' => $countryCode,
            'compare' => '='
        ),
    )
];

if ("" !== $univCode) {

    $user_args["meta_query"][] = array(
        'key' => 'verifier_assign_univ',
        'value' => $univCode,
        'compare' => 'LIKE'
    );

}

$users = get_users($user_args);

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.