get_comments_number

The timeline below displays how wordpress function get_comments_number has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Retrieves the amount of comments a post has.
 *
 * @since 1.5.0
 *
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is the global `$post`.
 * @return string|int If the post exists, a numeric string representing the number of comments
 *                    the post has, otherwise 0.
 */
function get_comments_number($post = 0)
{
    $post = get_post($post);
    $comments_number = $post ? $post->comment_count : 0;
    $post_id = $post ? $post->ID : 0;
    /**
     * Filters the returned comment count for a post.
     *
     * @since 1.5.0
     *
     * @param string|int $comments_number A string representing the number of comments a post has, otherwise 0.
     * @param int        $post_id Post ID.
     */
    return apply_filters('get_comments_number', $comments_number, $post_id);
}

WordPress Version: 6.1

/**
 * Retrieves the amount of comments a post has.
 *
 * @since 1.5.0
 *
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is the global `$post`.
 * @return string|int If the post exists, a numeric string representing the number of comments
 *                    the post has, otherwise 0.
 */
function get_comments_number($post = 0)
{
    $post = get_post($post);
    $count = $post ? $post->comment_count : 0;
    $post_id = $post ? $post->ID : 0;
    /**
     * 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);
}

WordPress Version: 4.9

/**
 * Retrieves the amount of comments a post has.
 *
 * @since 1.5.0
 *
 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is the global `$post`.
 * @return string|int If the post exists, a numeric string representing the number of comments
 *                    the post has, otherwise 0.
 */
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);
}

WordPress Version: 4.6

/**
 * Retrieve the amount of comments a post has.
 *
 * @since 1.5.0
 *
 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
 * @return int The number of comments a post has.
 */
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 int $count   Number of comments a post has.
     * @param int $post_id Post ID.
     */
    return apply_filters('get_comments_number', $count, $post_id);
}

WordPress Version: 4.0

/**
 * Retrieve the amount of comments a post has.
 *
 * @since 1.5.0
 *
 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global $post.
 * @return int The number of comments a post has.
 */
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;
    }
    /**
     * Filter the returned comment count for a post.
     *
     * @since 1.5.0
     *
     * @param int $count   Number of comments a post has.
     * @param int $post_id Post ID.
     */
    return apply_filters('get_comments_number', $count, $post_id);
}

WordPress Version: 3.9

/**
 * Retrieve the amount of comments a post has.
 *
 * @since 1.5.0
 *
 * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
 * @return int The number of comments a post has.
 */
function get_comments_number($post_id = 0)
{
    $post_id = absint($post_id);
    if (!$post_id) {
        $post_id = get_the_ID();
    }
    $post = get_post($post_id);
    if (!isset($post->comment_count)) {
        $count = 0;
    } else {
        $count = $post->comment_count;
    }
    /**
     * Filter the returned comment count for a post.
     *
     * @since 1.5.0
     *
     * @param int         $count   Nnumber of comments a post has.
     * @param int|WP_Post $post_id Post ID or WP_Post object.
     */
    return apply_filters('get_comments_number', $count, $post_id);
}

WordPress Version: 3.8

/**
 * Retrieve the amount of comments a post has.
 *
 * @since 1.5.0
 *
 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default current post.
 * @return int The number of comments a post has.
 */
function get_comments_number($post_id = 0)
{
    $post_id = absint($post_id);
    if (!$post_id) {
        $post_id = get_the_ID();
    }
    $post = get_post($post_id);
    if (!isset($post->comment_count)) {
        $count = 0;
    } else {
        $count = $post->comment_count;
    }
    /**
     * Filter the returned comment count for a post.
     *
     * @since 1.5.0
     *
     * @param int         $count   The number of comments a post has.
     * @param int|WP_Post $post_id The post ID or WP_Post object.
     */
    return apply_filters('get_comments_number', $count, $post_id);
}

WordPress Version: 3.7

/**
 * Retrieve the amount of comments a post has.
 *
 * @since 1.5.0
 *
 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default current post.
 * @return int The number of comments a post has.
 */
function get_comments_number($post_id = 0)
{
    $post_id = absint($post_id);
    if (!$post_id) {
        $post_id = get_the_ID();
    }
    $post = get_post($post_id);
    if (!isset($post->comment_count)) {
        $count = 0;
    } else {
        $count = $post->comment_count;
    }
    /**
     * Filter the returned comment count for a post.
     *
     * @since 1.5.2
     *
     * @param int         $count   The number of comments a post has.
     * @param int|WP_Post $post_id The post ID or WP_Post object.
     */
    return apply_filters('get_comments_number', $count, $post_id);
}