Home / comment / Count parent comments and replies separately
  • How to Count parent comments and replies separately

    count-parent-comments-and-replies-separately

    This snippet will count parent comments and replies separately. Add the first snippet to the functions.php of your wordpress theme then add the second snippet in the single.php or the theme you will to display the number of comments and comment replies.

    ( click code to copy )

    wordpress snippet : PHP - functions.php

    <>
    function c_parent_comment_counter($id){
        global $wpdb;
        $query = "SELECT COUNT(comment_post_id) AS count FROM $wpdb->comments WHERE `comment_approved` = 1 AND `comment_post_ID` = $id AND `comment_parent` = 0";
        $parents = $wpdb->get_row($query);
        return $parents->count;
    }
    
    ( click code to copy )

    wordpress snippet : PHP

    <>
    <?php
    $number_of_parents = c_parent_comment_counter($post->ID);
    $number_of_children = $post->comment_count - $number_of_parents;
    echo "parents: ".$number_of_parents;
    echo "children: ".$number_of_children;
    ?>
    

    ( WordPress codex functions, hooks, in this snippet. )

    query, comment_post, wp,