Search WordPress Users by Name

If you run a WordPress site with a large list of users and have ever needed to search for a specific user (from the Users > All Users admin page) there’s a good chance no users were found. The problem is that the default query only searches by nicename (username) and email address. However, if you need to search by first name, last name, or a custom user meta field, the default search query is all but useless. Thankfully, the Improved user search in backend plugin fixes this shortcoming, but unfortunately this plugin is outdated and won’t work if you’re running PHP 5.5 and WordPress 3.9 or higher.

So, I’ve gone ahead and created my own user query filter to fix the problem, and included some additional functionality to improve the usefulness of the user search.

 

This can simply be dropped into your theme’s functions.php file, or can be used to create a plugin. It will work as is, but if you have custom user meta fields, possibly added using Advanced Custom Fields, you can simply add the field key to the $usermeta_keys array to include it in the user query.

Join the Conversation

4 Comments

  1. Why would I have got this… ?

    Warning: Cannot modify header information – headers already sent by (output started at /home/mysite/public_html/wp-content/themes/mytheme/functions.php:68) in /home/mysite/public_html/wp-includes/pluggable.php on line 1210

    1. Sorry for the very late reply, my email notifications weren’t coming through. I’m not sure why, without being able to look at your theme’s functions.php. This specific error happens when something has been output to the output buffer (using echo, var_dump, etc) and then an attempt to modify the header is made. Generally it’s just changing the order in which the code is executed, but I’m not sure of the specific problem.

  2. Hi John,
    I can’t seem to get this working at all on Multisite.
    The specific site is a child, blog_id 2.
    Even when I specify only one custom usermeta field name in usermeta_keys (‘geo_city’) and then enter a search term I know to be present, no results are returned.

    With other methods of achieving the same goal, extending User search to custom field (https://wordpress.org/plugins/better-user-search/ & #3 @ https://rudrastyh.com/wordpress/pre_user_query.html), I have experienced a very different problem… the functionality works, they return results, but the listing/pagination is all broken.

    With your method, however, I don’t get any results at all.

    I think Multisite and/or pre_user_query are the culprits, possible in both kinds of scenario.

    Do you have any insights you can share?

    Many thanks.

  3. Hello Laubsterboy,

    Thank you for this 🙂

    I’m having a strange issue in which my custom usermeta ‘member_id’ works perfectly with your code, but ‘first_name’, ‘shipping_first_name’, etc. do not. Those return zero results.

    The only difference I can see is that my custom ‘member_id’ meta value is always a number, whereas the ‘first_name’, ‘shipping_first_name’, etc. are strings. Other than that, everything else is exactly the same between them.

    I’ll keep at it and will post here if I find a solution, but I was wondering if you might have a clue as to what the issue could be. Thanks!

Leave a comment

Your email address will not be published. Required fields are marked *

You can also subscribe without commenting.