0

I am new user of WP_User_Query(). I don't know how to express whole situation. If I use below code I am getting good result.

$users = new WP_User_Query(array(
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'last_name',
                'value' => $_POST['search'],
                'compare' => 'LIKE',
            ),      
        )
    ));

If I use below code I am getting good result also.

$users = new WP_User_Query(array(
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'organisation',
                'value' => 519,
                'compare' => 'LIKE',
            ),          
        )
    ));

I am trying to combine these two query like below.

$users = new WP_User_Query(array(
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'last_name',
                'value' => $_POST['search'],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'organisation',
                'value' => 519,
                'compare' => 'LIKE',
            ),          
        )
    ));

But I am not getting expected output.

How can I combine these two queries ?

6
  • 1
    Have you tried changing ‘ OR’ to ‘AND’? Jan 16 at 10:31
  • Thanks @JacobPeattie. How to use ‘ OR’ to ‘AND’ ? I am new.
    – Foysal
    Jan 16 at 10:34
  • 1
    Literally in your code just change it. Jan 16 at 10:39
  • 1
    "I am not getting expected output" - then what output were you expecting? Users with a last name that matches the search query and that the users have a meta named organisation with the value 519? "Can I use here 'compare' => '=', ?" - yes you can (in fact, that's the default compare value), but whether it would return any results would depend on your meta value (e.g. if the value is a number, then = would return one or more results if there are users with organisation meta where the value is exactly 519).
    – Sally CJ
    Jan 17 at 2:40
  • 1
    Thanks @SallyCJ. Your comment is informative. Thanks.
    – Foysal
    Jan 19 at 2:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.