Search Admin User List on First and Last Names

This code requires WordPress 3.1 and above!

Add this function and hook to your functions.php to enable searching the first_name and last_name fields on the Admin Users page.  Also enables sorting by lastname, firstname using the special ‘byname:’ prefix to the sort text.

By default, the ‘Search Users’ box on the Admin Users page only checks fields present in wp_users.  This function uses the pre_user_query hook to add wp_usermeta rows having the meta_key of either ‘first_name’ or ‘last_name’ to the FROM clause.  Then, it modifies the WHERE clause to add the check for the search term.

Here is the code:

 * Modify the User Search in Admin to include first, last names.
 * Add sorting by name if search string starts with 'byname:'.
function mam_pre_user_query($user_search) {
   global $wpdb;
   // print_r($user_search);
   $vars = $user_search->query_vars;
   if (!is_null($vars['search'])) {
      /* For some reason, the search term is enclosed in asterisks.
         Remove them */
      $search = preg_replace('/^\*/','',$vars['search']);
      $search = preg_replace('/\*$/','',$search);
      $user_search->query_from .= " INNER JOIN {$wpdb->usermeta} m1 ON " .
         "{$wpdb->users}.ID=m1.user_id AND (m1.meta_key='first_name')";
      $user_search->query_from .= " INNER JOIN {$wpdb->usermeta} m2 ON " .
         "{$wpdb->users}.ID=m2.user_id AND (m2.meta_key='last_name')";

      // IF the search var starts with byname:, sort by name.
      if (preg_match('/^byname:/',$search)) {
         $search = preg_replace('/^byname:/','',$search);
         $user_search->query_orderby = ' ORDER BY UPPER(m2.meta_value), UPPER(m1.meta_value) ';
         $user_search->query_vars['search'] = $search;
         $user_search->query_where = str_replace('byname:','',$user_search->query_where);
      $names_where = $wpdb->prepare("m1.meta_value LIKE '%s' OR m2.meta_value LIKE '%s'",
      $user_search->query_where = str_replace('WHERE 1=1 AND (',
         "WHERE 1=1 AND ({$names_where} OR ",$user_search->query_where);
   //print_r('<br />SEARCH OBJECT: ');print_r($user_search);
   //print_r('<br />SEARCH TERM: ');print_r($search);
   //print_r('<br />QUERY_FROM: ');print_r($user_search->query_from);
   //print_r('<br />NAMES_WHERE: ');print_r($names_where);
   //print_r('<br />QUERY_WHERE: ');print_r($user_search->query_where);

7 Responses to Search Admin User List on First and Last Names

  • Gerry says:

    Thanks, great post! Just one Q: how do I get the little sorting arrow to appear next to the column name in wpadmin -> Users ?

    • Mac McDonald says:

      Sorry for the delay in replying – something prevented the email that a new comment had been posted.

      This code does not use the WP column sort feature, so the arrow will not appear.

  • SONAM BHATIA says:

    Very much helpful and very nice work.. GREAT.

  • Ryan says:

    Thanks for posting the script. I noticed it only handled first or last name but not both together, so I made a small modification. Maybe someone else might find it useful.

    Replace Lines 27 and 28 with:

    		$names = explode(' ',$search,2);
    		if(count($names) &gt; 1){
    			$first_name = $names[0];
    			$last_name = $names[1];
    			$names_where = $wpdb-&gt;prepare("m1.meta_value LIKE '%s' OR m2.meta_value LIKE '%s' OR (m1.meta_value LIKE '%s' AND m2.meta_value LIKE '%s')", "%{$search}%","%$search%","%$first_name%","%$last_name%");
    		   $names_where = $wpdb-&gt;prepare("m1.meta_value LIKE '%s' OR m2.meta_value LIKE '%s'", "%{$search}%","%$search%");
  • masman says:

    Is there anyway to further modify this to create a custom user column for last_name that’s sortable? I’ve found plugins that will create the column, but nothing so far that sorts it alphabetically. Any help would be greatly appreciated!

    • Mac says:

      The Users panel shows the name already. Just add this to your functions.php and enter ‘byname:‘ in the ‘Search Users’ box. The list will be sorted on last, first name.

Leave a Reply

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