2

How do I get total number of Authors on the site?

This shows summery of users on the site but I just want to get number of Authors.

<?php
$result = count_users();
echo 'There are ', $result['total_users'], ' total users';
foreach($result['avail_roles'] as $role => $count)
    echo ', ', $count, ' are ', $role, 's';
echo '.';
?>

https://codex.wordpress.org/Function_Reference/count_users

2

4 Answers 4

4

You can use the WP_User_Query class, like the example below. Each code line have an small description, that you understand, what we do.

// Get all users with role Author.
$user_query = new WP_User_Query( array( 'role' => 'Author' ) );
// Get the total number of users for the current query. I use (int) only for sanitize.
$users_count = (int) $user_query->get_total();
// Echo a string and the value
echo 'So much authors: ' . $users_count;

Alternative you can also use the function get_users(). But is only a wrapper for the query and have much more fields in the result.

2

If you just want one field and/or a count, you can use get_users(). Restricting the fields returned makes for a fast query.

$users_count = count( get_users( array( 'fields' => array( 'ID' ), 'role' => 'author' ) ) );
1

If you just want authors count

 <?php
$result = count_users();
echo count( get_users( array( 'role' => 'author' ) ) );
?>
6
  • 2
    Why do you waste time for count_users() if you never use it?
    – fuxia
    Commented Aug 27, 2017 at 9:50
  • @fuxia - Is count( get_users( array( 'role' => 'author' ) ) ) faster performing than count_users()?
    – Motivated
    Commented Sep 5, 2022 at 7:33
  • @Motivated Probably not, but count_users() doesn't allow a limitation for a certain role.
    – fuxia
    Commented Sep 5, 2022 at 9:33
  • @fuxia, do you mean like $user_counts['avail_roles']['author'] or not allowing a limit as to the count which could result in a performance hit?
    – Motivated
    Commented Sep 5, 2022 at 9:55
  • @Motivated The array access comes after the query. If you have many users, you want to fetch only the data you really need, not all of them.
    – fuxia
    Commented Sep 5, 2022 at 14:51
0

The WP class WP_User_Query is inefficient if all you want is a count. The constructor runs query() producing an unnecessary call to users table, then a subsequent call with SELECT FOUND_ROWS().

After reviewing the wp core code we implement this filter.

add_filter('users_pre_query', function($result, WP_User_Query $user_query) {

   global $wpdb;
   $user_count = $wpdb->get_var("SELECT COUNT(1) {$user_query->query_from} {$user_query->query_where}");
   if(empty($wpdb->last_error)) $user_query->total_users = (int)$user_count;

   return false;
}, 10, 2);

The filter prevents local results from running and correctly sets total_users for use of:

$user_query = new WP_User_Query();
$user_query->get_total();

You'll want to remove the filter after instantiating.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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