WordPress WP_User_Query search

I am trying to write a user search query, i have location and searching keyword (user name)

i need to search all user, with last name or first name matches to searching keyword for a
specified loaction
my code here,

Read More
$args = array(
    'meta_query' => array(
        'relation' => 'AND',
        0 => array(
            'key'     => 'last_name',
            'value'   => $search_string,
            'compare' => 'LIKE'
        ),
        1 => array(
            'key'     => 'first_name',
            'value'   => $search_string,
            'compare' => 'LIKE'
        ),
        2 => array(
            'key'     => 'user_city',
            'value'   => $Location,
            'compare' => 'LIKE'
        ),
    ),

 );

$users = new WP_User_Query( $args);
$users = $users->get_results();

but i want ‘relation’ OR for first name and last name and relation AND for location, how i can do it ?

Related posts

Leave a Reply

3 comments

  1. please use

    $args = array( 'meta_query' => 
        array(
        array( 
           'relation' => 'OR', 
           array( 'key' => 'last_name', 'value' => $search_string, 'compare' => 'LIKE' ), 
           array( 'key' => 'first_name', 'value' => $search_string, 'compare' => 'LIKE' ),
        ), 
        array( 'key' => 'user_city', 'value' => $Location, 'compare' => 'LIKE' ), ), 
    );
    
  2. Try this

    $user_ex = explode(' ', $search_string);
        if($user_ex[1]) {
        $name_array = preg_split("/[s,]+/", $search_string_user);
            $args = new WP_User_Query( array(
            'meta_query' => array(
                'relation' => 'OR',
                array(
                    'relation' => 'OR',
                    array(
                        'key' => 'user_city',
                        'value' => $search_string,
                        'compare' => 'LIKE'
                    ),
                ),
                array(
                    'relation' => 'AND',
                    array(
                        'key' => 'first_name',
                        'value' => $name_array[0],
                        'compare' => 'LIKE'
                    ),
                    array(
                        'key' => 'last_name',
                        'value' => $name_array[1],
                        'compare' => 'LIKE'
                    ),
                ),
            ),
        }