count_users

Count number of users who have each of the user roles.

Description

(array) count_users( (string) $strategy = 'time' ); 

Assumes there are neither duplicated nor orphaned capabilities meta_values. Assumes role names are unique phrases. Same assumption made by WP_User_Query" title="WP_User_Query" class="code class">WP_User_Query::prepare_query() Using $strategy = time this is CPU-intensive and should handle around 10^7 users. Using $strategy = memory this is memory-intensive and should handle around 10^5 users, but see WP Bug #12257.

Returns (array)

Includes a grand total and an array of counts indexed by role strings.

Parameters (1)

0. $strategy — Optional. (string) => 'time'
'time' or memory

Usage

  1. if ( !function_exists( 'count_users' ) ) { 
  2. require_once ABSPATH . WPINC . '/user.php'; 
  3.  
  4. // 'time' or 'memory' 
  5. $strategy = 'time'; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = count_users($strategy); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/user.php  
  1. function count_users($strategy = 'time') { 
  2. global $wpdb; 
  3.  
  4. // Initialize 
  5. $blog_prefix = $wpdb->get_blog_prefix($id); 
  6. $result = array(); 
  7.  
  8. if ( 'time' == $strategy ) { 
  9. $avail_roles =wp_roles)->get_names(); 
  10.  
  11. // Build a CPU-intensive query that will return concise information. 
  12. $select_count = array(); 
  13. foreach ( $avail_roles as $this_role => $name ) { 
  14. $select_count[] = $wpdb->prepare( "COUNT(NULLIF(`meta_value` LIKE %s, false))", '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%'); 
  15. $select_count[] = "COUNT(NULLIF(`meta_value` = 'a:0:{}', false))"; 
  16. $select_count = implode(', ', $select_count); 
  17.  
  18. // Add the meta_value index to the selection list, then run the query. 
  19. $row = $wpdb->get_row( "SELECT $select_count, COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N ); 
  20.  
  21. // Run the previous loop again to associate results with role names. 
  22. $col = 0; 
  23. $role_counts = array(); 
  24. foreach ( $avail_roles as $this_role => $name ) { 
  25. $count = (int) $row[$col++]; 
  26. if ($count > 0) { 
  27. $role_counts[$this_role] = $count; 
  28.  
  29. $role_counts['none'] = (int) $row[$col++]; 
  30.  
  31. // Get the meta_value index from the end of the result set. 
  32. $total_users = (int) $row[$col]; 
  33.  
  34. $result['total_users'] = $total_users; 
  35. $result['avail_roles'] =& $role_counts; 
  36. } else { 
  37. $avail_roles = array( 
  38. 'none' => 0,  
  39. ); 
  40.  
  41. $users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" ); 
  42.  
  43. foreach ( $users_of_blog as $caps_meta ) { 
  44. $b_roles = maybe_unserialize($caps_meta); 
  45. if ( ! is_array( $b_roles ) ) 
  46. continue; 
  47. if ( empty( $b_roles ) ) { 
  48. $avail_roles['none']++; 
  49. foreach ( $b_roles as $b_role => $val ) { 
  50. if ( isset($avail_roles[$b_role]) ) { 
  51. $avail_roles[$b_role]++; 
  52. } else { 
  53. $avail_roles[$b_role] = 1; 
  54.  
  55. $result['total_users'] = count( $users_of_blog ); 
  56. $result['avail_roles'] =& $avail_roles; 
  57.  
  58. if ( is_multisite() ) { 
  59. $result['avail_roles']['none'] = 0; 
  60.  
  61. return $result;