Reputation: 1495
I am making a search box, when a "first name or full name" is entered the result is a table listing the user_id, email, full name etc...
There can be more than one user with same "first name or full name" which in that cause I will need to loop through the result set.
The search also need to be flexible so that John Smith or John Smi or John will work.
e.g [firstname lastname] [search]
Is there a way in WordPress to do this or do I have to use MySQL?
Upvotes: 0
Views: 2400
Reputation: 1495
Thanks,
This is what I needed
$search_name = 'John Smith';
$name_array = preg_split("/[\s,]+/", $search_name);
$users = new WP_User_Query(array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'first_name',
'value' => $name_array[0],
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $name_array[1],
'compare' => 'LIKE'
)
)
));
$users_found = $users->get_results();
Upvotes: 1
Reputation: 7720
if you're writing your own search function, you need to use Wp_User_Query. You'll find a lot of examples in that page, but what you need is probably the most basic form, like this:
$users = new WP_User_Query( array(
'search' => '*'.esc_attr( $your_search_string ).'*',
'search_columns' => array(
'user_login',
'user_nicename',
'user_email',
'user_url',
),
) );
$users_found = $users->get_results();
However, if you want only to search for user in your backend, then you may use a plugin, there are many out there. This one works pretty well for what you need
Upvotes: 0