Fastest way to get the comment and ping total count for a post

I’m making my own comment template (like this) and I need to know how can I get the comment and ping count for the current post, maybe using a fast database query or something like that?

Note that I can’t use count($comments) or anything like that, because I’m not running the default comments_template() function which gets all the comments from the database. Instead I’m pulling only the newest 10 comments using get_comments().

Read More

$post->comment_count (apparently initialized by get_post) is close to what I’m looking for, but it counts both comments and pings 🙁

Related posts

Leave a Reply

1 comment

  1. you can use this custom function in the functions.php of the theme:

    /**
     * count for trackback, pingback, comment, pings
     *
     * embed like this:
     * fb_comment_type_count('pings');
     * fb_comment_type_count('comment');
     */
    if ( !function_exists('fb_comment_type_count') ) {
            function fb_get_comment_type_count( $type='all', $zero = false, $one = false, $more = false, $post_id = 0) {
                    global $cjd_comment_count_cache, $id, $post;
    
                    if ( !$post_id )
                            $post_id = $post->ID;
                    if ( !$post_id )
                            return;
    
                    if ( !isset($cjd_comment_count_cache[$post_id]) ) {
                            $p = get_post($post_id);
                            $p = array($p);
                            fb_update_comment_type_cache($p);
                    }
                    ;
                    if ( $type == 'pingback' || $type == 'trackback' || $type == 'comment' )
                            $count = $cjd_comment_count_cache[$post_id][$type];
                    elseif ( $type == 'pings' )
                            $count = $cjd_comment_count_cache[$post_id]['pingback'] + $cjd_comment_count_cache[$post_id]['trackback'];
                    else
                            $count = array_sum((array) $cjd_comment_count_cache[$post_id]);
    
                    return apply_filters('fb_get_comment_type_count', $count);
            }
    
            // comment, trackback, pingback, pings, all
            function fb_comment_type_count( $type='all', $zero = false, $one = false, $more = false, $post_id = 0 ) {
    
                    $number = fb_get_comment_type_count( $type, $zero, $one, $more, $post_id );
                    if ($type == 'all') {
                            $type_string_single = __('Kommentar', FB_BASIS_TEXTDOMAIN);
                            $type_string_plural = __('Kommentare', FB_BASIS_TEXTDOMAIN);
                    } elseif ($type == 'pings') {
                            $type_string_single = __('Ping und Trackback', FB_BASIS_TEXTDOMAIN);
                            $type_string_plural = __('Pings und Trackbacks', FB_BASIS_TEXTDOMAIN);
                    } elseif ($type == 'pingback') {
                            $type_string_single = __('Pingback', FB_BASIS_TEXTDOMAIN);
                            $type_string_plural = __('Pingbacks', FB_BASIS_TEXTDOMAIN);
                    } elseif ($type == 'trackback') {
                            $type_string_single = __('Trackback', FB_BASIS_TEXTDOMAIN);
                            $type_string_plural = __('Trackbacks', FB_BASIS_TEXTDOMAIN);
                    } elseif ($type == 'comment') {
                            $type_string_single = __('Kommentar', FB_BASIS_TEXTDOMAIN);
                            $type_string_plural = __('Kommentare', FB_BASIS_TEXTDOMAIN);
                    }
    
                    if ( $number > 1 )
                            $output = str_replace('%', number_format_i18n($number), ( false === $more ) ? __('%', FB_BASIS_TEXTDOMAIN) . ' ' . $type_string_plural : $more);
                    elseif ( $number == 0 )
                            $output = ( false === $zero ) ? __('Keine', FB_BASIS_TEXTDOMAIN) . ' ' . $type_string_plural : $zero;
                    else // must be one
                            $output = ( false === $one ) ? __('Ein', FB_BASIS_TEXTDOMAIN) . ' ' . $type_string_single : $one;
    
                    echo apply_filters('fb_comment_type_count', $output, $number);
            }
    }
    

    this function give you the count of pingback, trackback, comments or all, example:

    <h2 class="comments"><?php fb_comment_type_count( 'comment' ); ?></h2>
    

    you can use the follow parameter for return the counter: comment, trackback, pingback, pings or all