{"id":289,"date":"2015-07-10T08:49:59","date_gmt":"2015-07-10T14:49:59","guid":{"rendered":"http:\/\/laubsterboy.com\/blog\/?p=289"},"modified":"2015-07-10T11:51:16","modified_gmt":"2015-07-10T17:51:16","slug":"search-wordpress-users-by-name","status":"publish","type":"post","link":"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/","title":{"rendered":"Search WordPress Users by Name"},"content":{"rendered":"<p>If you run a WordPress site with\u00a0a large list of users and have ever needed to search for a specific user (from the Users &gt; All Users admin page) there&#8217;s a good chance no users\u00a0were found. The problem is that the default query only searches by nicename (username) and email address. However,\u00a0if 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\u00a0<a href=\"https:\/\/wordpress.org\/plugins\/improved-user-search-in-backend\/\">Improved user search in backend<\/a>\u00a0plugin fixes this shortcoming, but unfortunately this plugin is outdated and won&#8217;t work if you&#8217;re running PHP 5.5 and WordPress 3.9 or higher.<\/p>\n<p>So, I&#8217;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.<\/p>\n<pre class=\"lang:php decode:true \">&lt;?php\r\n\r\n\/**\r\n* user_search_by_multiple_parameters\r\n* \r\n* Modifies the wp_user_query to allow for User searching (within the WordPress dashboard &gt; Users &gt; All Users) by:\r\n*\tfirst_name\r\n*\tlast_name\r\n*\tnickname\r\n*\tany other custom meta_key added to user profiles (manually or through something like Advanced Custom Fields)\r\n*\r\n* @param    object  $wp_user_query  a WordPress query object\r\n*\r\n* @return   object\t$wp_user_query  a modified version of the WordPress query object parameter\r\n*\/\r\nfunction user_search_by_multiple_parameters($wp_user_query) {\r\n    if (false === strpos($wp_user_query-&gt;query_where, '@') &amp;&amp; !empty($_GET[\"s\"])) {\r\n        global $wpdb;\r\n\r\n        $user_ids = array();\r\n        $user_ids_per_term = array();\r\n\r\n\t\t\/\/ Usermeta fields to search\r\n\t\t$usermeta_keys = array('first_name', 'last_name', 'nickname');\r\n\r\n\t\t$query_string_meta = \"\";\r\n\t\t$search_terms = $_GET[\"s\"];\r\n\t\t$search_terms_array = explode(' ', $search_terms);\r\n\t\t\r\n\t\t\/\/ Search users for each search term (word) individually\r\n\t\tforeach ($search_terms_array as $search_term) {\r\n\t\t\t\/\/ reset ids per loop\r\n\t\t\t$user_ids_per_term = array();\r\n\t\t\t\r\n\t\t\t\/\/ add all custom fields into the query\r\n\t\t\tif (!empty($usermeta_keys)) {\r\n\t\t\t\t$query_string_meta = \"meta_key='\" . implode(\"' OR meta_key='\", $wpdb-&gt;escape($usermeta_keys)) . \"'\";\r\n\t\t\t}\r\n\r\n\t\t\t\/\/ Query usermeta table\r\n            $usermeta_results = $wpdb-&gt;get_results($wpdb-&gt;prepare(\"SELECT DISTINCT user_id FROM $wpdb-&gt;usermeta WHERE (\" . $query_string_meta . \") AND LOWER(meta_value) LIKE '%%%s%%'\", $search_term));\r\n\r\n            foreach ($usermeta_results as $usermeta_result) {\r\n\t            if (!in_array($usermeta_result-&gt;user_id, $user_ids_per_term)) {\r\n                \tarray_push($user_ids_per_term, $usermeta_result-&gt;user_id);\r\n                }\r\n            }\r\n\t\t\t\r\n\t\t\t\/\/ Query users table\r\n            $users_results = $wpdb-&gt;get_results($wpdb-&gt;prepare(\"SELECT DISTINCT ID FROM $wpdb-&gt;users WHERE LOWER(user_nicename) LIKE '%%%s%%' OR LOWER(user_email) LIKE '%%%s%%' OR LOWER(display_name) LIKE '%%%s%%'\", $search_term, $search_term, $search_term));\r\n\r\n            foreach ($users_results as $users_result) {\r\n                if (!in_array($users_result-&gt;ID, $user_ids_per_term)) {\r\n                    array_push($user_ids_per_term, $users_result-&gt;ID);\r\n                }\r\n            }\r\n            \r\n            \/\/ Limit results to matches of all search terms\r\n            if (empty($user_ids)) {\r\n\t            $user_ids = array_merge($user_ids, $user_ids_per_term);\r\n            } else {\r\n                if (!empty($user_ids_per_term)) {\r\n                    $user_ids = array_unique(array_intersect($user_ids, $user_ids_per_term));\r\n                }\r\n            }\r\n        }\r\n\t\t\r\n\t\t\/\/ Convert IDs to comma separated string\r\n        $ids_string = implode(',', $user_ids);\r\n\r\n\t\tif (!empty($ids_string)) {\r\n\t\t\t\/\/ network users search (multisite)\r\n\t\t\t$wp_user_query-&gt;query_where = str_replace(\"user_nicename LIKE '\" . $search_terms . \"'\", \"ID IN(\" . $ids_string . \")\", $wp_user_query-&gt;query_where);\r\n\t\t\t\r\n\t\t\t\/\/ site (blog) users search\r\n            $wp_user_query-&gt;query_where = str_replace(\"user_nicename LIKE '%\" . $search_terms . \"%'\", \"ID IN(\" . $ids_string . \")\", $wp_user_query-&gt;query_where);\r\n            \r\n            \/\/ network\/site users search by number (WordPress assumes user ID number)\r\n            $wp_user_query-&gt;query_where = str_replace(\"ID = '\" . $search_terms . \"'\", \"ID = '\" . $search_terms . \"' OR ID IN(\" . $ids_string . \")\", $wp_user_query-&gt;query_where);\r\n\t\t}\r\n    }\r\n\r\n    return $wp_user_query;\r\n}\r\nadd_action('pre_user_query', 'user_search_by_multiple_parameters');\r\n\r\n?&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>This can simply be dropped into your theme&#8217;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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you run a WordPress site with\u00a0a large list of users and have ever needed to search for a specific user (from the Users &gt; All Users admin page) there&#8217;s a good chance no users\u00a0were found. The problem is that the default query only searches by nicename (username) and email address. However,\u00a0if you need to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[83,90,91,88,89,94,92,87,93,86,64],"class_list":["post-289","post","type-post","status-publish","format-standard","hentry","category-guide","tag-email","tag-first-name","tag-last-name","tag-name","tag-nicename","tag-pre_user_query","tag-query","tag-search","tag-usermeta","tag-users","tag-wordpress"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Search WordPress Users by Name<\/title>\n<meta name=\"description\" content=\"Search WordPress Users by Name is a guide detailing how to use the pre_user_query WordPress filter to search for users by name or any other usermeta fields.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Search WordPress Users by Name\" \/>\n<meta property=\"og:description\" content=\"Search WordPress Users by Name is a guide detailing how to use the pre_user_query WordPress filter to search for users by name or any other usermeta fields.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/\" \/>\n<meta property=\"og:site_name\" content=\"John Russell Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-07-10T14:49:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-07-10T17:51:16+00:00\" \/>\n<meta name=\"author\" content=\"John Russell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@laubsterboy\" \/>\n<meta name=\"twitter:site\" content=\"@laubsterboy\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Russell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/\"},\"author\":{\"name\":\"John Russell\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c\"},\"headline\":\"Search WordPress Users by Name\",\"datePublished\":\"2015-07-10T14:49:59+00:00\",\"dateModified\":\"2015-07-10T17:51:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/\"},\"wordCount\":202,\"commentCount\":6,\"keywords\":[\"Email\",\"First Name\",\"Last Name\",\"Name\",\"Nicename\",\"pre_user_query\",\"Query\",\"Search\",\"usermeta\",\"Users\",\"WordPress\"],\"articleSection\":[\"Guide\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/\",\"url\":\"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/\",\"name\":\"Search WordPress Users by Name\",\"isPartOf\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#website\"},\"datePublished\":\"2015-07-10T14:49:59+00:00\",\"dateModified\":\"2015-07-10T17:51:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c\"},\"description\":\"Search WordPress Users by Name is a guide detailing how to use the pre_user_query WordPress filter to search for users by name or any other usermeta fields.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.johnrussell.dev\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Search WordPress Users by Name\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#website\",\"url\":\"https:\/\/www.johnrussell.dev\/blog\/\",\"name\":\"John Russell Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.johnrussell.dev\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c\",\"name\":\"John Russell\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/00ce0f258fcc5e5d29897a5e81316009713e9104cfaf49f481c5bce3f81c7cb1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/00ce0f258fcc5e5d29897a5e81316009713e9104cfaf49f481c5bce3f81c7cb1?s=96&d=mm&r=g\",\"caption\":\"John Russell\"},\"url\":\"https:\/\/www.johnrussell.dev\/blog\/author\/laubsterboy\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Search WordPress Users by Name","description":"Search WordPress Users by Name is a guide detailing how to use the pre_user_query WordPress filter to search for users by name or any other usermeta fields.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/","og_locale":"en_US","og_type":"article","og_title":"Search WordPress Users by Name","og_description":"Search WordPress Users by Name is a guide detailing how to use the pre_user_query WordPress filter to search for users by name or any other usermeta fields.","og_url":"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/","og_site_name":"John Russell Blog","article_published_time":"2015-07-10T14:49:59+00:00","article_modified_time":"2015-07-10T17:51:16+00:00","author":"John Russell","twitter_card":"summary_large_image","twitter_creator":"@laubsterboy","twitter_site":"@laubsterboy","twitter_misc":{"Written by":"John Russell","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/#article","isPartOf":{"@id":"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/"},"author":{"name":"John Russell","@id":"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c"},"headline":"Search WordPress Users by Name","datePublished":"2015-07-10T14:49:59+00:00","dateModified":"2015-07-10T17:51:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/"},"wordCount":202,"commentCount":6,"keywords":["Email","First Name","Last Name","Name","Nicename","pre_user_query","Query","Search","usermeta","Users","WordPress"],"articleSection":["Guide"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/","url":"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/","name":"Search WordPress Users by Name","isPartOf":{"@id":"https:\/\/www.johnrussell.dev\/blog\/#website"},"datePublished":"2015-07-10T14:49:59+00:00","dateModified":"2015-07-10T17:51:16+00:00","author":{"@id":"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c"},"description":"Search WordPress Users by Name is a guide detailing how to use the pre_user_query WordPress filter to search for users by name or any other usermeta fields.","breadcrumb":{"@id":"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.johnrussell.dev\/blog\/2015\/07\/search-wordpress-users-by-name\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.johnrussell.dev\/blog\/"},{"@type":"ListItem","position":2,"name":"Search WordPress Users by Name"}]},{"@type":"WebSite","@id":"https:\/\/www.johnrussell.dev\/blog\/#website","url":"https:\/\/www.johnrussell.dev\/blog\/","name":"John Russell Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.johnrussell.dev\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/296c0c6bd1deeeb20834393e1e16325c","name":"John Russell","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.johnrussell.dev\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/00ce0f258fcc5e5d29897a5e81316009713e9104cfaf49f481c5bce3f81c7cb1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/00ce0f258fcc5e5d29897a5e81316009713e9104cfaf49f481c5bce3f81c7cb1?s=96&d=mm&r=g","caption":"John Russell"},"url":"https:\/\/www.johnrussell.dev\/blog\/author\/laubsterboy\/"}]}},"_links":{"self":[{"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/posts\/289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/comments?post=289"}],"version-history":[{"count":7,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/posts\/289\/revisions"}],"predecessor-version":[{"id":292,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/posts\/289\/revisions\/292"}],"wp:attachment":[{"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/media?parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/categories?post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.johnrussell.dev\/blog\/wp-json\/wp\/v2\/tags?post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}