Have you ever think you can display users count based on role, this post will show you how you can display user count based user role. WordPress has a builtin function for count user but you can’t display user count by role.
How to count user?
You will need to use count_users();
get count data available on your site, you will be able to find user count by role from this.
$user_count_data = count_users(); $avail_roles = $user_count_data['avail_roles']; foreach ($avail_roles as $role_key=>$role_count){ echo $role_key.':'.$role_count.'<br/>'; }
Out put will like this based on user
administrator:2 author:4 none:0
If you know the “role” you can also display directly the count value.
$user_count_data = count_users(); $avail_roles = $user_count_data['avail_roles']; $author = $avail_roles['author']; /* User role author */ echo $author $administrator = $avail_roles['administrator']; /* User role administrator */ echo $administrator;
How to count total user?
$user_count_data = count_users(); $total_users = $user_count_data['total_users']; echo $total_users;
If you are managed community blogs this will help you lot by displaying user count. to learn more the function please see details hereĀ count_users.