1

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:

enter image description here

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.

2
  • Do you need this to be integrated into the main search or can it be a stand-alone search function?
    – s_ha_dum
    May 8, 2013 at 22:02
  • I need it to be stand-alone - search results will display on different page other than search.php
    – Romes
    May 8, 2013 at 22:04

1 Answer 1

3

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 very WP_Query-like class that should let you do everything you want including search for user metadata from the $wpdb->usermeta table by passing a meta_query parameter similar to the following from the Codex:

$args = array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'country',
            'value' => 'Israel',
            'compare' => '='
        ),
        array(
            'key' => 'age',
            'value' => array( 20, 30 ),
            'type' => 'numeric',
            'compare' => 'BETWEEN'
        )
    )
 );
$user_query = new WP_User_Query( $args );

If you have trouble, edit the question to include your (attempt at working) code and more detail, and I will edit the answer.

Your Answer

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

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