get_comments_number() WordPress Function

The get_comments_number() function is used to get the number of comments for a given post. This function is useful for displaying the number of comments on a post, or for checking if a post has any comments.

get_comments_number( int|WP_Post $post_id ) #

Retrieves the amount of comments a post has.


Parameters

$post_id

(int|WP_Post)(Optional) Post ID or WP_Post object. Default is the global $post.


Top ↑

Return

(string|int) If the post exists, a numeric string representing the number of comments the post has, otherwise 0.


Top ↑

Source

File: wp-includes/comment-template.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 );
}


Top ↑

Changelog

Changelog
VersionDescription
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More