I need to create a search page that will display anything related to the supplied search.
i.e. comments
containing it, events
, posts
, CPT
s and users
with that name.
How can I search for users in the site who’s first or last name contains the search phrase ?
Searching the main table
Simply use
WP_User_Query
with a search argument.So if you want to search for example for a user with a keyword in his
user_email
or similar columns from the{$wpdb->prefix}users
table, then you can do the following:Keep in mind that
*
is a wildcard. So restricting for example theuser_email
to a single domain would give you the following search string:*@example.com
.The
search
string has some “magic” features: Thesearch_columns
defaults to…user_email
if@
is present in thesearch
arg.user_login
andID
if thesearch
arg is numericuser_url
if thesearch
string containshttp://
orhttps://
user_login
anduser_nicename
if a string is present.All those defaults are only set if no
search_columns
argument was specified.Searching the meta table
If you want to search by for example
first_name
orlast_name
, then you’ll have to do ameta_query
as they’re not part of the main table:Make sure you retrieve the right search string. Normally that would be
get_query_var('s');
, but it could – depending on your formname/id
as well be something different that you might want to retrieve using$_GET['user_search']
for example. Make sure to properly esacpe it and remove unwanted white space from the beginning and end of the string.Keep in mind that this is an
array( array() )
as there’s therelation
key. If you just want to have a single key searched, it might be easier to just go with the following:Final query
The result might look close to the following:
This helped me instead of kaiser’s answer: https://laubsterboy.com/blog/2015/07/search-wordpress-users-by-name/
But in this solution
$wpdb->escape($usermeta_keys)
function generated an error, so I simply used$usermeta_keys
.Custom solution
I know my answer is late, but i was looking for the same thing and build it with some help of @kaiser his answer. I’m only going to display the title, link and excerpt/user email, but you can expand this in the way you want offcourse.
Custom array
I started by creating an empty array:
User search
Then moved on with the ‘WP_User_Query’ which uses the user-input ($search_results) for filtering.
Fill our array with user data
Now we gonna fill this query in our empty ($search_results) array.
Fill our array with post/page data
We also want the ‘normal’ search form that searches for pages and post to still work, so we need to loop over this as well. While we do this, we are also gonna fill our ($search_results) array with this data:
Loop over our array
Now we have an array that contains user info and post/page details.
The last thing we need to do now is looping over it, so the user sees the results he searched for:
Done!
Recently when I was working on a large scale project, my clients were facing problems while searching users with their first name, last name, business name/company name.
So I thought, lets create a plugin and combine my front-end skills with Php. I decided to go with VueJs and Axios to accomplish my mission.
https://wordpress.org/plugins/robust-user-search/
This plugin allow users to search by : Username, First name, Last name, Email or Business Name.
If you are using simpler get_user() function, you can use the following code: