11

I need to create a search page that will display anything related to the supplied search. i.e. comments containing it, events, posts, CPTs and users with that name.

How can I search for users in the site who's first or last name contains the search phrase ?

22

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:

$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();

Keep in mind that * is a wildcard. So restricting for example the user_email to a single domain would give you the following search string: *@example.com.

The search string has some "magic" features: The search_columns defaults to...

  • user_email if @ is present in the search arg.
  • user_login and ID if the search arg is numeric
  • user_url if the search string contains http:// or https://
  • or ... user_login and user_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 or last_name, then you'll have to do a meta_query as they're not part of the main table:

$search_string = esc_attr( trim( get_query_var('s') ) );
$users = new WP_User_Query( array(
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'first_name',
            'value'   => $search_string,
            'compare' => 'LIKE'
        ),
        array(
            'key'     => 'last_name',
            'value'   => $search_string,
            'compare' => 'LIKE'
        )
    )
) );
$users_found = $users->get_results();

Make sure you retrieve the right search string. Normally that would be get_query_var('s');, but it could - depending on your form name/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 the relation key. If you just want to have a single key searched, it might be easier to just go with the following:

$search_string = esc_attr( trim( get_query_var('s') ) );
$users = new WP_User_Query( array(
    'meta_key'     => 'first_name',
    'meta_value'   => $search_string,
    'meta_compare' => 'LIKE',
) );
$users_found = $users->get_results();

Final query

The result might look close to the following:

$search_string = esc_attr( trim( get_query_var('s') ) );
$users = new WP_User_Query( array(
    'search'         => "*{$search_string}*",
    'search_columns' => array(
        'user_login',
        'user_nicename',
        'user_email',
        'user_url',
    ),
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => 'first_name',
            'value'   => $search_string,
            'compare' => 'LIKE'
        ),
        array(
            'key'     => 'last_name',
            'value'   => $search_string,
            'compare' => 'LIKE'
        )
    )
) );
$users_found = $users->get_results();
  • In which file i want to add this query? – Naveen Jan 27 '14 at 7:04
  • @Naveen Best fit would be either the functions.php, your template or (which would be best) wrapped up nicely in a custom mini-plugin to not loose functionality when switching themes. – kaiser Jan 27 '14 at 10:54
  • Kiaser thanks for your reply. please help me how can i integrate this code to functions.php – Naveen Jan 27 '14 at 11:11
  • 3
    @Naveen That's out of scope here. You'll have to learn a bit about code or find a developer to do that for you. And read that Q/A. – kaiser Jan 27 '14 at 11:17
  • display_name is a column in wp_users. Two questions come to mind. 1: Is this a recent change to the database schema? 2: Can it be used as a value in search_columns instead of the meta query? – henrywright Aug 13 '17 at 9:08
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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