How to Display Comment Ping Count in WordPress

Home » Snippets » How to Display Comment Ping Count in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you interested in monitoring how many times your blog post has been referenced in the comments section of other websites? With the help of comment ping count, WordPress can keep track of the number of pingbacks and trackbacks on your site. In this article, we’ll explain what comment ping count is, how it works in WordPress, and how you can make the most of this feature to stay on top of your post engagement.

					function wpturbo_comment_ping_count() {
    global $wpdb;

    // Get the count of pending comments for the current post
    $count = $wpdb->get_var(
        $wpdb->prepare(
            "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '0' AND comment_type = ''",
            get_the_ID()
        )
    );

    // Get the count of pending pings for the current post
    $ping_count = $wpdb->get_var(
        $wpdb->prepare(
            "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '0' AND comment_type = 'pingback'",
            get_the_ID()
        )
    );

    // Output the total count of comments and pings
    if ( $count && $ping_count ) {
        echo sprintf(
            _n( '%1$s comment (%2$s ping)', '%1$s comments (%2$s pings)', $count, 'wpturbo' ),
            number_format_i18n( $count ),
            number_format_i18n( $ping_count )
        );
    } elseif ( $count ) {
        printf( _n( '%d comment', '%d comments', $count, 'wpturbo' ), number_format_i18n( $count ) );
    } elseif ( $ping_count ) {
        printf( _n( '%d ping', '%d pings', $ping_count, 'wpturbo' ), number_format_i18n( $ping_count ) );
    }
}
				

The code snippet shown above is a function that fetches the count of pending comments and pending pings for the current post in WordPress. The function, wpturbo_comment_ping_count(), uses the global $wpdb object to query the database to get the count of pending comments and pending pings.

First, the code queries the database to get the count of pending comments for the current post. It uses get_the_ID() function to get the ID of the current post, then uses a prepared SQL statement to count the pending comments that are associated with the current post. It filters by comment_type being empty and comment_approved being equal to 0 which means it’s an unapproved comment. The count is stored in $count variable.

Next, the function queries the database to get the count of pending pings for the current post. It again uses get_the_ID() function to get the ID of the current post, then uses a prepared SQL statement to count the pending pings that are associated with the current post by filtering the comment_type with pingback parameter. The count is stored in the $ping_count variable.

Finally, the function outputs the total count of comments and pings using the conditionals and printf() function. It checks if both $count and $ping_count exist. If so, it uses _n() to format the count for singular or plural case and sprintf() to substitute and replace %1$s and %2$s placeholders with the counts of comments and pings respectively. The result is a singular phrase when $count or $ping_count is equal to 1, and plural when $count or $ping_count is greater than 1.

If $count isn’t zero but $ping_count is zero, it prints a singular or plural phrase for comments.

If $count is zero but $ping_count isn’t zero, it prints a singular or plural phrase for pings.

Overall, this function is useful for displaying the count of pending comments and pending pings for the current post, which can be helpful information for the admin or author to handle the comment approval process and track the engagement with the post.

Register an account to save your snippets or go Pro to get more features.