count_users [ WordPress Function ]
| Parameters: |
|
| Returns: |
|
| Defined at: |
|
Count number of users who have each of the user roles.
Assumes there are neither duplicated nor orphaned capabilities meta_values. Assumes role names are unique phrases. Same assumption made by 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.
Source
<?php
function count_users($strategy = 'time') {
global $wpdb, $wp_roles;
// Initialize
$id = get_current_blog_id();
$blog_prefix = $wpdb->get_blog_prefix($id);
$result = array();
if ( 'time' == $strategy ) {
global $wp_roles;
if ( ! isset( $wp_roles ) )
$wp_roles = new WP_Roles();
$avail_roles = $wp_roles->get_names();
// Build a CPU-intensive query that will return concise information.
$select_count = array();
foreach ( $avail_roles as $this_role => $name ) {
$select_count[] = "COUNT(NULLIF(`meta_value` LIKE '%\"" . like_escape( $this_role ) . "\"%', false))";
}
$select_count = implode(', ', $select_count);
// Add the meta_value index to the selection list, then run the query.
$row = $wpdb->get_row( "SELECT $select_count, COUNT(*) FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'", ARRAY_N );
// Run the previous loop again to associate results with role names.
$col = 0;
$role_counts = array();
foreach ( $avail_roles as $this_role => $name ) {
$count = (int) $row[$col++];
if ($count > 0) {
$role_counts[$this_role] = $count;
}
}
// Get the meta_value index from the end of the result set.
$total_users = (int) $row[$col];
$result['total_users'] = $total_users;
$result['avail_roles'] =& $role_counts;
} else {
$avail_roles = array();
$users_of_blog = $wpdb->get_col( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '{$blog_prefix}capabilities'" );
foreach ( $users_of_blog as $caps_meta ) {
$b_roles = maybe_unserialize($caps_meta);
if ( ! is_array( $b_roles ) )
continue;
foreach ( $b_roles as $b_role => $val ) {
if ( isset($avail_roles[$b_role]) ) {
$avail_roles[$b_role]++;
} else {
$avail_roles[$b_role] = 1;
}
}
}
$result['total_users'] = count( $users_of_blog );
$result['avail_roles'] =& $avail_roles;
}
return $result;
}
?>
Examples [ wp-snippets.com ]
Google Arama Sonuçlarý
- Function Reference/count users « WordPress Codex
The call to count_users returns the number of users with each role. It will not return any roles having count == 0, so the results are intended to be used in foreach ...
codex.wordpress.org - Docs for page user.php
count_users (line 841). Count number of users who have each of the user roles. Assumes there are neither duplicated nor orphaned capabilities meta_values.
phpdoc.wordpress.org - Count_users | WP Code Snippets
Apr 30, 2011 ... It can be useful to display certain stats to our visitors. Today, we'll construct a function and corresponding shortcode that can display the number ...
wpcodesnippets.info - count_users | A HitchHackers guide through WordPress
Feb 11, 2011 ... function count_users($strategy = 'time') {}. Count number of users who have each of the user roles. Assumes there are neither duplicated nor ...
hitchhackerguide.com