WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 1 down vote accepted

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:

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>';
            $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>';
        $re .= '
            <option value="nickname">Name</option>
            <option value="areacode">area code</option>
            <option value="company">company</option>
            <option value="affiliate">affiliate</option>';
    $re .= '
        <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" />
    return $re;


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

share|improve this answer
Thank you so much, this is almost exactly what I am looking for... but what if you wanted to allow partial search for example on the company name. Search for all companies that start with the letter B or in the case of the affiliate field where it would contain a few characters somewhere in the field. – Gregg Jul 18 '11 at 15:39
@Gregg for that you would have to create a custom SQL query and use %LIKE% parameter. – Bainternet Jul 18 '11 at 21:01

There are some plugins that you can take give a try, it may not be exact, but it should cover your needs:

Search everything http://wordpress.org/extend/plugins/search-everything/

faceted search http://wordpress.org/extend/plugins/faceted-search/

wp custom field search http://wordpress.org/extend/plugins/wp-custom-fields-search/

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.