Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to find a function, plugin, query - something that will separate the parent comment count from the reply count.

Does anyone know how to achieve this?

10 comments total are broken into 3 parent comments and 7 replies. How can I display this at the top of the comment form?

Please & thank you!

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

here's a quick and dirty method...

in functions php, we create a callback function for wp_list_comments() which will count our comments:

$GLOBALS['parent_comments_count'] = 0;
$GLOBALS['child_comments_count'] = 0;

function count_comments( $comment, $args, $depth ) {
    $GLOBALS['comment'] = $comment;
    if($comment->comment_parent==0):
        $GLOBALS['parent_comments_count']++;
    else:
        $GLOBALS['child_comments_count']++;
    endif;
}

then, in our template where it calls wp_list_comments(), we add another with a callback to our count function, then immediately after we can echo our count. then we have the regular wp_list_comments(), which will actually output the comments:

wp_list_comments( array( 'callback' => 'count_comments' ) );
echo "parents: ". $GLOBALS['parent_comments_count'];
echo "children: ". $GLOBALS['child_comments_count'];
wp_list_comments();

emphasis on 'dirty', I'm sure there's a cleaner way to do this.

Here's another method which uses an additional query. This will get numbers regardless of pagination. We use the comment_count variable in $post to subtract number of parents from to get number of children.

In 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;
}

In the template:

<?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;

?>
share|improve this answer
1  
That is perfect, thank you so much! I am paginating comments over 100, and it appears to count them separately per page, any way to do a total count regardless of pagination? –  Leanne May 5 '11 at 16:34
1  
I've edited the answer with another method that should work with paginated comments. It uses an additional SQL query to count just parent comments. –  Milo May 5 '11 at 17:34
1  
OH my gosh, Milo. You have no idea how grateful I am. THANK you thank you thank you. I posted over in the WP forums but no reply there - are you a moderator? Can I link from there to here for others, or copy/paste your fix? –  Leanne May 5 '11 at 19:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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