doubinduo3364 2016-12-04 06:14
浏览 106
已采纳

使用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;
}
  • 写回答

2条回答 默认 最新

  • dounei9043 2016-12-04 06:45
    关注

    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'
            )
            )
        );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。