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>';
}