wordpress user search firstname lastname and display name code

wordpress user search firstname lastname and display name code

In wordpress admin users listing page, wordpress admin has user search with username and email but we cannot search via display name or first name and last name.

We can add firstname, lastname and display name in users search. You just need to add following code.

/*
 * functions will be applicable for only wordpress admin
 */
if (is_admin()) {
 /*
 * Modify the User Search in Admin to include firstname, lastname and display_name
 */
 add_action('pre_user_query', 'wpapi_pre_user_query');

function wpapi_pre_user_query($user_search) {
 //die();
 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);
 //print_r($search);
 //search in display name
 if(!empty($search)){
 $user_search->query_where = substr(trim($user_search->query_where), 0, -1) . " OR display_name LIKE '%". $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')";
 $names_where = $wpdb->prepare("m1.meta_value LIKE '%s' OR m2.meta_value LIKE '%s'", "%{$search}%", "%$search%");
 $user_search->query_where = str_replace('WHERE 1=1 AND (', "WHERE 1=1 AND ({$names_where} OR ", $user_search->query_where);
 }
 return $user_search;
 }

}

wordpress user search firstname lastname and display name code
wordpress user search firstname lastname and display name code

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

6 thoughts on “wordpress user search firstname lastname and display name code”

  1. The above code does work in adding First and Last names into the user search, BUT, in WP 4.1 it breaks the role search when clicking on any of the roles in the user admin menu.. Subscribers, Members, and whatever other roles you might have set up..

    Any Fix for this?

    1. Found an alternative.. I don’t normally recommend plugins, but in order to retain the role search capabilities, I ended up adding the search function of this one to my functions.php instead..

      https://wordpress.org/plugins/full-name-search-in-wp-admin/

      Was a bit skeptical at first as it’s brand new and I was only the 13th downloader.. But the code looks rather light weight while adding both full and/or partial search capabilities to username, first, last, first-last, and email..

      With a few tweaks looks like you could even add search for other custom meta tags if you wanted, but I was just looking for something simple and lightweight, and this seems to do the job..

Leave a Reply

Your email address will not be published.