How to Count Parent Comments and Replies Separately in WordPress

Home » Snippets » How to Count Parent Comments and Replies Separately 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

If you run a website with active comments, you might want to know how many of them are replies to other comments and how many are top-level comments. Unfortunately, WordPress doesn’t provide a built-in way to do this, but fear not – there’s a solution. In this article, we’ll show you how to count parent comments and replies separately in WordPress. This will enable you to better understand the engagement on your website and make informed decisions about how to manage your comments section.

					function wpturbo_count_parent_comments_and_replies_separately() {
    global $wpdb;

    $comments = $wpdb->get_results(
        "
        SELECT comment_approved, COUNT(*) AS total
        FROM $wpdb->comments
        WHERE comment_post_ID = $postID
        AND comment_type = ''
        GROUP BY comment_approved
        "
    );

    $parent_comments = 0;
    $parent_comments_approved = 0;
    $replies = 0;
    $replies_approved = 0;

    foreach ( $comments as $comment ) {
        if ( $comment->comment_approved == 1 ) {
            if ( $comment->comment_parent == 0 ) {
                $parent_comments_approved += $comment->total;
            } else {
                $replies_approved += $comment->total;
            }
        } else {
            if ( $comment->comment_parent == 0 ) {
                $parent_comments += $comment->total;
            } else {
                $replies += $comment->total;
            }
        }
    }

    echo '<p>Parent comments: ' . $parent_comments . ' (' . $parent_comments_approved . ' approved)</p>';
    echo '<p>Replies: ' . $replies . ' (' . $replies_approved . ' approved)</p>';
}
add_action( 'wp', 'wpturbo_count_parent_comments_and_replies_separately' );
				

The code snippet above explains how to count parent comments and replies separately in WordPress. This means we’ll be able to display the number of parent comments and their approved counterparts, as well as the number of replies and their corresponding approved comments.

We start by defining the function wpturbo_count_parent_comments_and_replies_separately(), which is responsible for counting the comments and organizing them correctly. We use the global variable $wpdb to perform a custom SQL query that retrieves all comments for the current post only. We make sure to exclude pingbacks, trackbacks, and other non-standard types of comments by checking for an empty comment type field.

$comments = $wpdb->get_results(
    "
    SELECT comment_approved, COUNT(*) AS total
    FROM $wpdb->comments
    WHERE comment_post_ID = $postID
    AND comment_type = ''
    GROUP BY comment_approved
    "
);

The query counts all comments and groups them by their approval status, which can be either 0 or 1. Zero means the comment is not approved, whereas one means the comment is approved.

After the comments have been retrieved, we create four variables to count the number of parent comments and replies and their corresponding approved comments. We loop through the comments with the help of a foreach loop, and test each comment’s comment_approved and comment_parent fields to determine the status of each comment.

If the comment is approved and has no parent (comment_parent == 0), it’s considered a parent comment, and we add its count to the total count of approved parent comments. If it’s approved but has a parent, it’s considered a reply, and we add its count to the total count of approved replies.

If the comment is not approved and has no parent, it’s also considered a parent comment, and we add its count to the total count of unapproved parent comments. Similarly, if it’s not approved and has a parent, it’s considered a reply, and we add its count to the total count of unapproved replies.

Finally, we display the counts on the front-end using the echo function. We concatenate the variables to create user-friendly messages that show the number of parent comments and replies and their corresponding approved comments.

We add this function to the wp action to make sure it gets executed when WordPress is fully loaded.

And that’s it! You can use this function in your WordPress installation to display separated counts of parent comments and replies across your theme or plugin.

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