Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

3 Answers

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;
share|improve this answer
    
Where do I place the code? I placed it in footer and it returned: FATAL ERROR: CALL TO A MEMBER FUNCTION GET_RESULTS() ON A NON-OBJECT IN... Please help :) –  jimilesku Jul 31 '12 at 17:34
    
See edit. Forgot to rename a var. –  kaiser Jul 31 '12 at 18:00
2  
Answer is ok but this one is a bit shorter: count( get_users( array( 'role' => 'author' ) ) ) –  Jan Beck Jul 31 '12 at 18:07
    
@JanBeck I placed the echo before the code You wrote and it works perfectly :) Thank You so much! –  jimilesku Jul 31 '12 at 18:12
1  
Then add it as answer - will get a +1 from me. I tend to use classes for answer, when they're considered also a public API (WP mixes there a lot), as you can show more possibilities. –  kaiser Jul 31 '12 at 18:12
show 1 more comment
echo count( get_users( array( 'role' => 'author' ) ) )
share|improve this answer
add comment

count_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';
share|improve this answer
1  
Can You please give me the working examples for counting authors and counting subscribers for I am not that good in coding :) Thank You so very much! –  jimilesku Jul 31 '12 at 14:05
1  
@jimilesku Please always search in Codex (helps you learning), when someone only posts a Template Tag. Thanks. –  kaiser Jul 31 '12 at 18:14
    
@kaiser I am doing my best, thank YOu for the input :) Learning every day something new :) –  jimilesku Jul 31 '12 at 18:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.