2016-12-04 77 views
0

我正在嘗試開發一個目錄搜索。以下工作正常與姓氏搜索一樣。但是,我需要最後和名字一起搜索。我怎樣才能做到這一點:使用WP_User_Query進行名字和姓氏搜索

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; 
} 

回答

2

您可以通過多箇中繼欄像下面的元搜索查詢,

$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' 
     ) 
     ) 
    ); 
+0

完美,謝謝。 –

+0

不客氣 –