I’m trying to build a custom search form for one of my pages. I need the search form to be able to search for users. Not only will it need to search for users, but by first name, last name etc.. Something similar to this:
The default WP search doesn’t allow for me to search users on my site. I’m using the plugin ‘Search Everything’ and still can’t manage to get any user data to show up in my searches.
Any help is much appreciated.
EDIT
Here is my form:
<?php
global $wpdb;
$authors = $wpdb->get_results("SELECT ID, display_name FROM $wpdb->users WHERE user_nicename != 'admin' ORDER BY display_name LIMIT 10");
?>
<form action="<?php bloginfo('siteurl'); ?>" id="memberform" method="get">
<div class="first_name">
<select id="firstNameDrop" name="s">
<option selected value="First Name">First Name</option>
<?php
foreach($authors as $author) {
$author_info = get_userdata($author->ID);
$name = $author_info->first_name;
echo '<option value="'.$name.'">'.$name.'</option>';
}
?>
</select>
</div>
<input type="submit" class="submit" name="submit" value="Search" />
</form>
This allows me to have my dropdown box with all of the users sorted by first name, and when I hit search, it takes me to my search page and pulls all posts corresponding to my search. That’s all fine and dandy, but I need it to ONLY pull in the users information – such as first name, last name, avatar, description etc.. and NOT posts.
I am not going to write your form for you, but create a form and submit it to a page to do the processing, or use the AJAX API. Then use
WP_User_Query
to actually search for users. It is a veryWP_Query
-like class that should let you do everything you want including search for user metadata from the$wpdb->usermeta
table by passing ameta_query
parameter similar to the following from the Codex:If you have trouble, edit the question to include your (attempt at working) code and more detail, and I will edit the answer.