get_comments_number

Retrieve the amount of comments a post has.

Description

(int) get_comments_number( (int) $post_id = 0 ); 

Returns (int)

The number of comments a post has.

Parameters (1)

0. $post_id — Optional. (int)
Post ID or WP_Post object. Default is global $post.

Usage

  1. if ( !function_exists( 'get_comments_number' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment-template.php'; 
  3.  
  4. // Optional. Post ID or WP_Post object. Default is global $post. 
  5. $post_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = get_comments_number($post_id); 
  9.  

Defined (1)

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

/wp-includes/comment-template.php  
  1. function get_comments_number( $post_id = 0 ) { 
  2. $post = get_post( $post_id ); 
  3.  
  4. if ( ! $post ) { 
  5. $count = 0; 
  6. } else { 
  7. $count = $post->comment_count; 
  8. $post_id = $post->ID; 
  9.  
  10. /** 
  11. * Filters the returned comment count for a post. 
  12. * @since 1.5.0 
  13. * @param int $count Number of comments a post has. 
  14. * @param int $post_id Post ID. 
  15. */ 
  16. return apply_filters( 'get_comments_number', $count, $post_id );