WordPress at a glance

get_comments_number() WP 1.0

Retrieves the amount of comments a post has.

✈ 1 time = 0.000631s = slow | 50000 times = 1.09s = fast
Hooks from function:
Return

String/Int. If the post exists, a numeric string representing the number of comments the post has, otherwise 0.

Usage

get_comments_number( $post_id );
$post_id(int/WP_Post)
Post ID or WP_Post object.
Default: global $post

Code of get_comments_number: wp-includes/comment-template.php VER 4.9.8

<?php
function get_comments_number( $post_id = 0 ) {
	$post = get_post( $post_id );

	if ( ! $post ) {
		$count = 0;
	} else {
		$count = $post->comment_count;
		$post_id = $post->ID;
	}

	/**
	 * Filters the returned comment count for a post.
	 *
	 * @since 1.5.0
	 *
	 * @param string|int $count   A string representing the number of comments a post has, otherwise 0.
	 * @param int        $post_id Post ID.
	 */
	return apply_filters( 'get_comments_number', $count, $post_id );
}

Related Functions

From category: Comments

No comments
    Hello, !     Log In . Register