1

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

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.

1

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' ) ) );
?>
  • Why do you waste time for count_users() if you never use it? – fuxia Aug 27 '17 at 9:50

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.