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.
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' => '%a%' ), 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!
Pretty neat!
$user_query = new WP_User_Query( array( 'search' => 'a' ) );
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' => '%a%' ), 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!
$user_query = new WP_User_Query( array( 'search' => '*a' ) ); // '%a' $user_query = new WP_User_Query( array( 'search' => 'a*' ) ); // 'a%' $user_query = new WP_User_Query( array( 'search' => '*a*' ) ); // '%a%
Pretty neat!
0 comments:
Post a Comment
Thanks for commenting. I will Reply you soon