You can use WP_User_Query() class to fetch users by various arguments. With it’s search parameter, you can search users by these columns 'ID', 'login', 'user_nicename', 'user_email', 'user_url'
. Although you’ve to specify which columns you want perform the search.
[php]$user_query = new WP_User_Query( array( ‘search’ => ‘Tareq’ ) );[/php]
When searching users with the search parameter, by default WordPress searches for direct match. It doesn’t search with any wildcard even if you pass like this: array( 'search' => '%Tareq%' )
, instead it’ll escape the percentage sign.
But there is a hidden gem by which you can search with wildcards. Just put *
at the beginning and/or in the end of your string. WordPress will replace *
with %
sign and you can search with wildcard. There is no documentation about this!
Example:
[php]$user_query = new WP_User_Query( array( ‘search’ => ‘*Tareq’ ) ); // ‘%Tareq’
$user_query = new WP_User_Query( array( ‘search’ => ‘Tareq*’ ) ); // ‘Tareq%’
$user_query = new WP_User_Query( array( ‘search’ => ‘*Tareq*’ ) ); // ‘%Tareq%[/php]
Pretty neat!