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(
array(
'role' => 'technician',
'search' => '*' . $search . '*',
'search_columns' => array(
'user_login',
'user_nicename',
'user_email',
'user_url',
'display_name'
)
)
);
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?
Thanks.
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;
} );
wp_users
table fields. That should be possible on theVARCHAR
fields, but it would need some adjustments to the database and modification how theWP_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:28display_name
column – DamianFox Sep 5 '15 at 19:19"*John Doe*"
translates into aLIKE '%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