admin管理员组文章数量:1327849
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();
本文标签: Find count of WordPress users by role and search string for user name
版权声明:本文标题:Find count of WordPress users by role and search string for user name 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742219811a2435242.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$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