0

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' ) ) ) 
2

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;
  • 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
2

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';
  • 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
  • I think this is the correct answer. Using WP_User_Query class is more expensive, as it returns all the users. On the other hand, get_users() only returns the stats your requested in your question. – Greeso Jun 2 '17 at 4:25
1
echo count( get_users( array( 'role' => 'author' ) ) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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