count_user_posts

Number of posts user has written.

Description

(string) count_user_posts( (int) $userid, (string) $post_type = 'post', (bool) $public_only = false ); 

Returns (string)

Number of posts the user has written in this post type.

Parameters (3)

0. $userid (int)
The userid.
1. $post_type — Optional. (string) => 'post'
Single post type or array of post types to count the number of posts for. Default post..
2. $public_only — Optional. (bool) => false
Whether to only return counts for public posts. Default false.

Usage

  1. if ( !function_exists( 'count_user_posts' ) ) { 
  2. require_once ABSPATH . WPINC . '/user.php'; 
  3.  
  4. // The userid. 
  5. $userid = -1; 
  6.  
  7. // Optional. Single post type or array of post types to count the number of posts for. Default 'post'. 
  8. $post_type = 'post'; 
  9.  
  10. // Optional. Whether to only return counts for public posts. Default false. 
  11. $public_only = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = count_user_posts($userid, $post_type, $public_only); 
  15.  

Defined (1)

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

/wp-includes/user.php  
  1. function count_user_posts( $userid, $post_type = 'post', $public_only = false ) { 
  2. global $wpdb; 
  3.  
  4. $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only ); 
  5.  
  6. $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ); 
  7.  
  8. /** 
  9. * Filters the number of posts a user has written. 
  10. * @since 2.7.0 
  11. * @since 4.1.0 Added `$post_type` argument. 
  12. * @since 4.3.1 Added `$public_only` argument. 
  13. * @param int $count The user's post count. 
  14. * @param int $userid User ID. 
  15. * @param string|array $post_type Single post type or array of post types to count the number of posts for. 
  16. * @param bool $public_only Whether to limit counted posts to public posts. 
  17. */ 
  18. return apply_filters( 'get_usernumposts', $count, $userid, $post_type, $public_only );