Using WordPress ‘get_comments_number’ PHP filter

The get_comments_number WordPress PHP filter allows you to modify the comment count for a post.

Usage

add_filter( 'get_comments_number', 'your_custom_function', 10, 2 );

function your_custom_function( $count, $post_id ) {
    // your custom code here
    return $count;
}

Parameters

  • $count (string|int) – A string representing the number of comments a post has, otherwise 0.
  • $post_id (int) – Post ID.

More information

See WordPress Developer Resources: get_comments_number

Examples

Display comments count in a different format

Modify the comments count to show in thousands format (e.g., 1k, 2.3k).

add_filter( 'get_comments_number', 'display_comments_count_in_thousands', 10, 2 );

function display_comments_count_in_thousands( $count, $post_id ) {
    if ( $count >= 1000 ) {
        $count = round( $count / 1000, 1 ) . 'k';
    }
    return $count;
}

Add custom text to comments count

Add custom text to the comments count, such as “50 comments” instead of “50”.

add_filter( 'get_comments_number', 'add_custom_text_to_comments_count', 10, 2 );

function add_custom_text_to_comments_count( $count, $post_id ) {
    $count .= ' comments';
    return $count;
}

Display comments count as “No Comments” for zero comments

Change the displayed text when there are no comments to “No Comments”.

add_filter( 'get_comments_number', 'display_no_comments_text', 10, 2 );

function display_no_comments_text( $count, $post_id ) {
    if ( $count == 0 ) {
        $count = 'No Comments';
    }
    return $count;
}

Increment comments count by a fixed number

Add a fixed number to the comments count (e.g., add 10 to the actual count).

add_filter( 'get_comments_number', 'increment_comments_count', 10, 2 );

function increment_comments_count( $count, $post_id ) {
    $count += 10;
    return $count;
}

Display only even-numbered comments count

Show comments count only if the count is even, otherwise display “Odd number of comments”.

add_filter( 'get_comments_number', 'display_even_comments_count', 10, 2 );

function display_even_comments_count( $count, $post_id ) {
    if ( $count % 2 != 0 ) {
        $count = 'Odd number of comments';
    }
    return $count;
}