5

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.

2 Answers 2

4

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]

2
  • 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, 2011 at 15:39
  • @Gregg for that you would have to create a custom SQL query and use %LIKE% parameter.
    – Bainternet
    Jul 18, 2011 at 21:01
1

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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