Creating a Front-end based User Search

I am working on a membership site which requires a front-end user search. I have been searching for an answer like a mad man, and I could really use some help! While there are several plugins that have this capability (WordPress Members List, AMR Users, etc.); none of them do exactly what I need, and by looking at their source codes, would be a huge mess to pick apart, and seems like it might a fruitless effort in the end anyway. I fully realize that I will likely need to write something custom myself here. The logic I need is actually very straight forward and simple in terms of how it needs to work.

User search logic goes like:

Read More

1. User enters another users USER ID # into a simple single search field.

2. Upon submitting search, the user is taken directly to the members profile

Yes, users will search for other users by their user ID (think of it as a membership number). There is a specific reason for this, which does make sense for the site. But I don’t want to get off the subject, just know that this requirement cannot be changed. I am thinking this shouldn’t matter though, because if you can query “user_nicename” from the “wp_users” table in the database, you should be able to query the “ID” field as well?

I do not expect anyone to provide me with the entire code for this obviously, but it would be nice if you could give me some general direction (and perhaps examples if possible) on how I can go about this. I am having a difficult time wrapping my head around this for some reason.

Thanks!

Related posts

Leave a Reply

2 comments

  1. As you haven’t offered the details on what you’re after, I’ll try to grab them all very briefly.

    Use the API – public WP_User_Query API functions

    Basically get_user_by() should be enough for you. Let’s say you fire of your form and the input field name was user_id. So you’d just retrieve the value from the $_REQUEST (or $_GET) and then do a simple query.

    $user = get_user_by( 'user_id', $_REQUEST['user_id'] );
    

    Advanced Task: Meta User Query

    If you’re going to do more complex requests (for maybe only a partly request that covers more than one user as possible result), then it’s time to call get_users(), which is the default API wrapper for new WP_User_Query.

    A meta query could look like this:

    $args = array(
        'meta_key' => 'custom-usermeta',
        'meta_value' => $cat_id,
        'meta_compare' => '='
    );
    $users = get_users( $args );
    

    AJAX Search

    If you need AJAX search functionality, just take a look at the answers filed under to get around it – no reason repeating it here.

    I have asked a question about autocomplete some time ago and covers the most important parts.

    // jQuery autocomplete - already bundled with WP core
    function wpse71343_scripts()
    {
        wp_enqueue_script( 'jquery-ui-autocomplete' );
    }
    add_action( 'wp_enqueue_scripts', 'wpse71343_scripts' );