0

I wish to display the number of trackbacks, pingbacks and comments a post has. I've read all about them and I am wondering if there is a simple solution, that I am not seeing, available.

I know that wp_count_comments (trac line 1188) searches in WPs cache before making a database query and I want to know if there is such a thing for pingbacks and trackbacks.

Right now I am using this modified code to show the number of comments, trackbacks, pingbacks and pings(trackbacks + pingbacks)

function commentCount( $type = 'pings' ) {
    global $wpdb;

    if ( $type == 'pings' ) {
        $typeSql = 'comment_type != ""';
    } elseif ( $type == 'trackbacks' ) {
        $typeSql = 'comment_type = "trackback"';
    } elseif ( $type == 'pingbacks' ) {
        $typeSql = 'comment_type = "pingback"';
    }

    $result = $wpdb->get_var( '
        SELECT
            COUNT(comment_ID)
        FROM
            ' . $wpdb->comments . '
        WHERE
            ' . $typeSql . ' AND
            comment_approved="1" AND
            comment_post_ID= ' . get_the_ID()
    );

    return $result;
}


$comments   = wp_count_comments( get_the_ID() );
$trackbacks = commentCount( 'trackbacks' );
$pingbacks  = commentCount( 'trackbacks' );
$pings      = commentCount( 'pings' );

1 Answer 1

1

I think you can use get_comments.

In the type parameter you can pass the type of comment (trackback, ping), you can filter by status, and you have a parameter called count, that when is set to true returns just the number of comments that match the parameters you passed.

 get_comments(
    array(
    'status' => 'approve',
    'post_id'=> get_the_ID(), 
    'type'=> 'pings', 
    'count' => true)
 );

This example gets the number of approved pings for a post, you have to use it inside a loop, if not, the function get_the_ID() wont work, but you just have to pass the ID as you want.

2
  • And after just echo $var;, right?
    – aifrim
    Commented Sep 16, 2014 at 18:45
  • yes, check the result, as if it's zero instead of showing "0 pingbacks", just don't show anything, but that is a design decision you have to make.
    – Tomás Cot
    Commented Sep 16, 2014 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.