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:
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!
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 functionsBasically
get_user_by()
should be enough for you. Let’s say you fire of your form and the input fieldname
wasuser_id
. So you’d just retrieve the value from the$_REQUEST
(or$_GET
) and then do a simple query.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 fornew WP_User_Query
.A meta query could look like this:
AJAX Search
If you need AJAX search functionality, just take a look at the answers filed under ajax 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.
I think the WordPress function you’re looking for is:
Of course you’ll have to generate the page with the results returned.