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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 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; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.