I want to get the total number of results after searching WordPress users by role and a string that matches the user name.
What I tried so far:
$args= array('echo'=>false, 'role' => 'author', 'search'=>$search_txt);
$user_query = new WP_User_Query($args);
$auth_count= $user_query->get_total();
But it returns 0 everytime.
Note:
Perhaps it can be done by:
$args= array('echo'=>false, 'role' => 'author', 'search'=>$search_txt);
$auth_count= count(get_users($args));
or by querying with
global $wpdb;
But is there any more resource friendly method?
I want to get the total number of results after searching WordPress users by role and a string that matches the user name.
What I tried so far:
$args= array('echo'=>false, 'role' => 'author', 'search'=>$search_txt);
$user_query = new WP_User_Query($args);
$auth_count= $user_query->get_total();
But it returns 0 everytime.
Note:
Perhaps it can be done by:
$args= array('echo'=>false, 'role' => 'author', 'search'=>$search_txt);
$auth_count= count(get_users($args));
or by querying with
global $wpdb;
But is there any more resource friendly method?
Share Improve this question edited Jul 29, 2020 at 18:55 sariDon asked Jul 29, 2020 at 18:28 sariDonsariDon 2651 gold badge2 silver badges18 bronze badges 2 |2 Answers
Reset to default 0Adding this as an answer here for reference for anyone searching for WP_User_Query stuff:
As per the docs The search
field on WP_User_Query
uses *
for wildcard text search, and will search on login
, nicename
, email
and URL
, unless specified otherwise.
E.g. to match any name containing bob
you need:
$args = array(
'search' => '*bob*'
);
$user_query = new WP_User_Query( $args );
This worked:
$search_txt
replaced by esc_attr($search_txt).'*'
:
$args= array('echo'=>false, 'role' => 'author', 'search'=>esc_attr($search_txt).'*');
$user_query = new WP_User_Query($args);
$auth_count= $user_query->get_total();
$search_txt
. I'm not sure what else would explain getting zero results unless it really is zero results for your search ;-) – mozboz Commented Jul 29, 2020 at 18:37