I'm using WP_User_Query to get user by display_name and other columns such as: user_login, user_nicename. Here is the code:

$my_users = new WP_User_Query(
                'role' => 'technician',
                'search' => '*' . $search . '*',
                'search_columns' => array(

It works if I search a user which display_name is John Doe, but if I search Doe John it obviously doesn't work. How can solve this problem?

P.S. : I have enabled the display_name search in functions.php file using this code:

 * Add support for the "display_name" search column in WP_User_Query
 * @see http://wordpress.stackexchange.com/a/166369/26350
add_filter( 'user_search_columns', function( $search_columns ) {
    $search_columns[] = 'display_name';
    return $search_columns;
} );
  • How would you handle the "John Smith Doe" search? Would you try all the 3! = 6 possibilities? What about 5 names? – birgire Sep 5 '15 at 17:36
  • It looks like you're looking for a fulltext search feature on some of the wp_users table fields. That should be possible on the VARCHAR fields, but it would need some adjustments to the database and modification how the WP_User_Query search works. You could also just split the search string and do a query for each name. – birgire Sep 5 '15 at 18:28
  • @birgire it works if the name searched is written as it is written in display_name column – DamianFox Sep 5 '15 at 19:19
  • yes because the "*John Doe*" translates into a LIKE '%John Doe%' kind of search on the varchar fields. PS: I just changed this into a code block to fix the comment formatting problem when using a star in the text ;-) – birgire Sep 5 '15 at 19:51

Your Answer

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

Browse other questions tagged or ask your own question.