count_many_users_posts

Number of posts written by a list of users.

Description

(array) count_many_users_posts( (array) $users, (string) $post_type = 'post', (bool) $public_only = false ); 

Returns (array)

Amount of posts each user has written.

Parameters (3)

0. $users (array)
Array of user IDs.
1. $post_type — Optional. (string) => 'post'
Single post type or array of post types to check. Defaults to post..
2. $public_only — Optional. (bool) => false
Only return counts for public posts. Defaults to false.

Usage

  1. if ( !function_exists( 'count_many_users_posts' ) ) { 
  2. require_once ABSPATH . WPINC . '/user.php'; 
  3.  
  4. // Array of user IDs. 
  5. $users = array(); 
  6.  
  7. // Optional. Single post type or array of post types to check. Defaults to 'post'. 
  8. $post_type = 'post'; 
  9.  
  10. // Optional. Only return counts for public posts. Defaults to false. 
  11. $public_only = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = count_many_users_posts($users, $post_type, $public_only); 
  15.  

Defined (1)

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

/wp-includes/user.php  
  1. function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) { 
  2. global $wpdb; 
  3.  
  4. $count = array(); 
  5. if ( empty( $users ) || ! is_array( $users ) ) 
  6. return $count; 
  7.  
  8. $userlist = implode( ', ', array_map( 'absint', $users ) ); 
  9. $where = get_posts_by_author_sql( $post_type, true, null, $public_only ); 
  10.  
  11. $result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N ); 
  12. foreach ( $result as $row ) { 
  13. $count[ $row[0] ] = $row[1]; 
  14.  
  15. foreach ( $users as $id ) { 
  16. if ( ! isset( $count[ $id ] ) ) 
  17. $count[ $id ] = 0; 
  18.  
  19. return $count;