admin管理员组文章数量:1303411
Question
I want to display total number of Authors and total number of subscribers on the blog but exclude the admin in this way:
56 Authors so far
15 Subscribers so far
Code that is close to what I need :)
I have this code and it displays the total number of everybody that is registered. I need it to display authors and subscriber roles separately. Please help :)
<?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
echo $users." members so far"; ?>
Answer updated:
Thank You @JanBeck This code generates the number of authors
echo count( get_users( array( 'role' => 'author' ) ) )
Question
I want to display total number of Authors and total number of subscribers on the blog but exclude the admin in this way:
56 Authors so far
15 Subscribers so far
Code that is close to what I need :)
I have this code and it displays the total number of everybody that is registered. I need it to display authors and subscriber roles separately. Please help :)
<?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
echo $users." members so far"; ?>
Answer updated:
Thank You @JanBeck This code generates the number of authors
echo count( get_users( array( 'role' => 'author' ) ) )
Share
Improve this question
edited Jul 31, 2012 at 18:16
jimilesku
asked Jul 31, 2012 at 13:10
jimileskujimilesku
2632 gold badges10 silver badges22 bronze badges
4 Answers
Reset to default 3count_users()
should give you an array of all the required user counts.
You can use it like this.
$user_counts = count_users();
$authors = $user_counts['avail_roles']['author']; //Get the author count
$subscribers = $user_counts['avail_roles']['subscriber']; //Get the subscriber count
echo $authors. ' Authors so far';
echo $subscribers. ' Subscribers so far';
The WP_User_Query class
There's the WP_User_Query
for exactly that. This class is an extension for the cores base wpdb
class. The counting will therefore be saved inside the global $wpdb;
object and is easy accessible.
global $wpdb;
$author_search = new WP_User_Query( array( 'role' => 'author' ) );
$author_list = $author_search->get_results();
$author_count = $wpdb->num_rows;
echo count( get_users( array( 'role' => 'author' ) ) )
you can try this attracts all members
$users = get_users();
foreach ($users as $get_users) {
echo $get_users->display_name . '<br>';
}
本文标签: List total number of users that are authors
版权声明:本文标题:List total number of users that are authors 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741751865a2395880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论