通过前端的自定义字段(user_meta)搜索用户 [英] Search user by custom fields ( user_meta ) in the front end

查看:239
本文介绍了通过前端的自定义字段(user_meta)搜索用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用WP_User_Query在前端显示了大量用户.

I have a large amount of users which I'm displaying on the front end with WP_User_Query.

我在此前端用户数据库上具有搜索功能,并且需要能够按自定义字段进行搜索,每个配置文件都有多个自定义字段.

I have a search function on this front end user database, and need to be able to search by custom fields, which each profile has a number of.

当前,搜索引擎将搜索标准的wp用户字段,例如"user_firstname","user_lastname",但不会搜索我的自定义字段(在此示例中为"institution"和"equipment"),不知道为什么.

Currently the search engine will search for standard wp user fields such as 'user_firstname', 'user_lastname', but won't search my custom fields ('institution' & 'equipment' in this example), and I'm not sure why.

查询:

 $search = ( isset($_GET['search-meta']) ) ? sanitize_text_field($_GET['search-meta']) : false ;

 if ($search){

    $my_users = new WP_User_Query( 

      array( 
        'role' => 'Subscriber', 
        'search' => '*' . $search . '*',
        'fields'     => 'all',    
        'meta_query' => array(
            'relation' => 'OR',
                array(
                    'key'     => 'institution',
                    'value'   => '*' . $search . '*',
                    'compare' => 'LIKE'
                ),
                array(
                    'key'     => 'equipment',
                    'value'   => '*' . $search . '*',
                    'compare' => 'LIKE'
                )
            )

      ));

  } 

搜索表单:

<form method="get" id="db-search" action="<?php the_permalink() ?>">

    <input type="text" class="field" name="search-meta" id="s" placeholder="Search Database" />

    <button type="submit"><i class="fa fa-search"></i></button>

</form>

推荐答案

您的代码中存在两个错误.

Two errors you have in your code.

    @cabrerahector在评论中提到的
  1. :您应该从meta_query value的所有内容中删除所有*.
  2. 您正在尝试在此处使用search:

  1. mentioned in the comment by @cabrerahector: you should remove all * from your meta_query value's.
  2. You're trying to use search here:

'role' => 'Subscriber', 
'search' => '*' . $search . '*',

但是问题是search键将始终尝试从 wp_users表中具有关联AND的电子邮件地址,URL,ID,用户名或显示名称.这意味着,如果您的$search与所提到的列之一不匹配,则您的查询将不返回任何内容.

But the problem is that search key will always try to find your $search from email address, URL, ID, username or display_name of your wp_users table with relation AND. This mean, that if your $search will not match with one of the mentioned columns, then your query will return nothing.

这是从用户meta_values中搜索的有效代码:

Here is working code to search from meta_values of users:

$search = ( isset($_GET['search-meta']) ) ? sanitize_text_field($_GET['search-meta']) : false ;

if ($search){

    $my_users = new WP_User_Query(

        array(
            'role' => 'Subscriber',
            'fields'     => 'all',
            'meta_query' => array(
                'relation' => 'OR',
                array(
                    'key'     => 'institution',
                    'value'   => $search,
                    'compare' => 'LIKE'
                ),
                array(
                    'key'     => 'equipment',
                    'value'   => $search,
                    'compare' => 'LIKE'
                )
            )

        ));

} 

这篇关于通过前端的自定义字段(user_meta)搜索用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆