使用WP_User_Query进行名字和姓氏搜索

I am trying to develop a directory search. The following works fine for last name search with like. But, I need last and first name to search together. How can I make that happen:

function dispDirectory()
{
  global $user_ID;

  // Disallow directory if there is a messaging administrator and this person is not it.
  if ($this->admin_user_id && $this->admin_user_id != $user_ID) {
    return '';
  }

    $directory = "<form name='form' method='post'><label for='clientname'>Client Name: </label>
    <input name='clientname' id='clientname' type='text' style='width: 181px;'
    value=''/><input type='submit'></form>";

    $clientname= $_POST["clientname"];

    $search_string = $clientname;

    $args  = array(
        'meta_key' => 'last_name',
        'meta_compare' => 'like',
        'meta_value' => $search_string,
    );

    $wp_user_query = new WP_User_Query($args);
    $wp_user_query->query_orderby = str_replace( 'user_login', 'wp_usermeta.meta_value', $wp_user_query->query_orderby );
    $wp_user_query->query();

    $users = $wp_user_query->get_results();


  foreach($users as $u)
  {
    $firstName = get_user_meta($u->ID, 'first_name', true);
    $lastName = get_user_meta($u->ID, 'last_name', true);         

    $directory .= '<p><strong>'.$lastName.', '.$firstName.'</strong> - <a href="'.$this->actionURL.'newmessage&to='.$u->ID.'">'.__('Send Message', 'cartpaujpm').'</a></p>';
  }
  return $directory;
}

You can search by multiple meta fields like below meta query,

$args  =  array ( 
        'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'first_name',
            'value'   => $search_string,
            'compare' => 'LIKE'
        ),
        array(
            'key'     => 'last_name',
            'value'   => $search_string,
            'compare' => 'LIKE'
        )
        )
    );

I recently came across a similar issue. Searching by name and needing to check:

wp_users table:

  • display_name,

wp_usermeta table:

  • first_name
  • last_name

What I do is take the entered search, break it into an array, and then check on each part:

if( ! empty( $searchword ) ){

    $parts = explode( ' ', $searchword );

    $qv['search_columns'] = array( 'display_name' );
    $qv['search'] = "*{$searchword}*";
    $qv['orderby']  = 'display_name';
    $qv['order']    = 'ASC';

    if( ! empty( $parts ) ){

        $qv['meta_query'] = [];
        $qv['meta_query']['relation'] = 'OR';

        foreach( $parts as $part ){
            $qv['meta_query'][] = array(
                'key'     => 'first_name',
                'value'   => $part,
                'compare' => 'LIKE'
            );
            $qv['meta_query'][] = array(
                'key'     => 'last_name',
                'value'   => $part,
                'compare' => 'LIKE'
            );
        }

    }

};

So with a search like "John Smith", we check for "John Smith" as the user's display_name, "John" as the first_name or last_name, and "Smith" as the first_name or last_name.

There's a similar example on the Codex: https://codex.wordpress.org/Class_Reference/WP_User_Query#Examples