How to search for users based on added user metadata

I have added several new fields to the user profile (areacode, company, affiliate, etc) and I would like to present a way to search on these fields. Please point me in the right direction.

Related posts

Leave a Reply

2 comments

  1. I just had a project where i needed to create a search by user meta form and i ended up creating a shortcode for it, i just altered it a bit to show you how to make it work with your fields, so just paste this code inside your theme’s functions.php file or a plugin file:

    add_shortcode('user_search','My_User_search');
    function My_User_search($atts = null){
        $out = user_search_form();
        if (isset($_GET['user_search']) && $_GET['user_search'] == "search_users" && isset($_GET['search_by'])){
            global $wpdb;
            $metakey = $_GET['search_by'];
            $args = array('meta_key' => $metakey);
             if (isset($_GET['s_value'])){
                $metavalue = $_GET['s_value'];
                $args['meta_value'] = $metavalue;
             }
    
    
            $search_users = get_users($args);
            $out .= '<div class="user_search_results">';
            if (count($search_users) >0){
    
                // here we loop over the users found and return whatever you want eg:
                $out .= '<ul>';
                foreach ($search_users as $user) {
                    $out .= '<li>' . $user->user_email . '</li>';
                }
                $out .= '</ul>';
            }else{
                $out .= 'No users found, try searching for something else.';
            }
            $out .= '</div>';
        }
        return $out;
    }
    
    //function to display user search form
    function user_search_form(){
        $metavalue = $metakey = '';
        if (isset($_GET['search_by'])){
            $metakey = $_GET['search_by'];
        }
        if (isset($_GET['s_value'])){
            $metavalue = $_GET['s_value'];
        }
        $re = '<div class="user_search"><form action="" name="user_s" method="get">
            <label for="search_by">Search by:</label>
                <select id="search_by" name="search_by">';
        if ($metakey != ''){
            $re.= '"';
            $re.= ($metakey == "nickname") ? '<option value="nickname" selected="selected">Name</option>': '<option value="nickname">Name</option>';
            $re.= ($metakey == "areacode") ? '<option value="areacode" selected="selected">area code</option>': '<option value="areacode">area code</option>';
            $re.= ($metakey == "company") ? '<option value="company" selected="selected">company</option>': '<option value="company">area code</option>';
            $re.= ($metakey == "affiliate") ? '<option value="affiliate" selected="selected">affiliate</option>': '<option value="affiliate">area code</option>';
        }else{
            $re .= '
                <option value="nickname">Name</option>
                <option value="areacode">area code</option>
                <option value="company">company</option>
                <option value="affiliate">affiliate</option>';
        }
        $re .= '
                </select>
            <label for="s_value">Value:</label>
                <input id="s_value" name="s_value" type="text" value="'.$metavalue.'"/>
                <input name="user_search" id="user_search" type="hidden" value="search_users"/>
                <input id="submit" type="submit" value="Search" />
            </form></div>';
        return $re;
    }
    

    Usage:

    simply create a page or a post and enter [user_search]