Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

2 Answers 2

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:

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]

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

 
discard

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.