Search user by custom fields ( user_meta ) in the front end

你。 提交于 2019-12-02 04:12:56

Two errors you have in your code.

  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 . '*',
    

    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.

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'
                )
            )

        ));

} 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!