1

I have put this code on author.php page

 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); 

 //gets the number of comment(s) on a post                  
 $comments_number = get_comments_number();

 //adds all the comment counts up
 $numbercomment += $comments_number;

 ?>
 <?php endwhile; ?>

 <span>A total of <?php echo $numbercomment; ?> comments have been made on this author posts.</span>

Currently i am using a loop to find number of a comments on a post and total it with all the other comment count on other posts. The consists of only the author's post so in that way i can find the number of comments people have made on that specific author's posts. Is there a simpler way of doing it.

EDIT:
I want to count the total number of comments people have made on all of that author's posts. Put the above code up in author.php file and you will get what i mean..

3
  • 2
    Possible duplicate of Comment Count for each Comment Author
    – bueltge
    Dec 14, 2016 at 7:10
  • 1
    @bueltge I want to count the comments people have made on that author's total posts Dec 14, 2016 at 8:38
  • OK, really different. Your Edit is helpful for this goal.
    – bueltge
    Dec 14, 2016 at 9:24

2 Answers 2

3

You could use get_comments(), this way you don't have to loop through posts.

<?php
    $args = array(
        'post_author' => '' // fill in post author ID
    );

    $author_comments = get_comments($args);

    echo count($author_comments);

?>
3
  • I want to count the total number of comments people have made on all of that author's posts Dec 14, 2016 at 8:40
  • Yes, that's what the code is supposed to do.
    – Pim
    Dec 14, 2016 at 12:05
  • Sorry my bad didn't check it.. Dec 14, 2016 at 13:15
1

Here I've written a SQL based function for you-

function the_dramatist_count_user_comments( $args = array() ) {
    global $wpdb;
    $default_args = array(
        'author_id' => 1,
        'approved' => 1
    );
    $param = wp_parse_args( $args, $default_args );
    $sql = $wpdb->prepare( "SELECT COUNT(comments.comment_ID) 
            FROM {$wpdb->comments} AS comments 
            LEFT JOIN {$wpdb->posts} AS posts
            ON comments.comment_post_ID = posts.ID
            WHERE posts.post_author = %d
            AND comment_approved = %d
            AND comment_type IN ('comment', '')",
        $param
    );

    return $wpdb->get_var( $sql );
}

Use it like below-

$author_posts_comments_count = the_dramatist_count_user_comments(
    array(
        'author_id' => 1, // Author ID
        'approved' => 1, // Approved or not Approved
    )
);

Now on $author_posts_comments_count you'll get the full comments count for an authors all posts.

It's tested.

Hope that helps.

3
  • I want to count the total number of comments people have made on all of that author's posts Dec 14, 2016 at 8:39
  • @MeghGandhi, Check the updated answer.
    – CodeMascot
    Dec 14, 2016 at 9:36
  • 1
    thanks you for your efforts.. But i would stick to using only php as i have forgotten sql XD Dec 14, 2016 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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