Show number of author’s posts and comments

When you run a site with more than one author, archive pages can become much more interesting. Instead of showing all content to your regular visitors, you can allow them to browse only through articles which have been written by their favorite author.

If you already use this neat WordPress feature, wouldn’t it be interesting if you could show your visitors the number of each author’s posts and comments even before they get to the archive pages?

While you can easily show author statistics in a widget, in today’s article, we’re about to show you how to display posts and comments count next to the author’s name manually. These numbers can show how active your author is and can interest casual reader into browsing his older work or it can stimulate a user to leave a question in the comments because the number of comments could suggest that the author likes to leave replies.

  1. Open single.php where you can display post and comment count for each author
  2. Copy and paste the code:
  3. global $wpdb;
    $user_id = $post->post_author;
    $where = 'WHERE comment_approved = 1 AND user_id = ' . $user_id ;
    $comment_count = $wpdb->get_var(
    "SELECT COUNT( * ) AS total
    FROM {$wpdb->comments}
    {$where}
    ");
    $user = get_userdata($user_id);
    $post_count = count_user_posts($user->ID);
    echo '<p>User ' . $user->display_name . ' has published ' . $post_count . ' posts, and the comment count is ' . $comment_count . '</p>';
  4. Change the text which will be displayed on the last line
  5. Style the text if needed
  6. Save changes

Even if you only copy and paste the code, without making extra modification, the code will display the number of author’s posts and comments. This simple feature can allow you to easily track those number from the frontend – while you, the admin, can easily see how active your authors are, your users will be able to see the same numbers and decide to browse through the author’s archive by clicking on the name link.

We hope that you like the new feature you have just installed. If you have questions, feel free to leave a comment below.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *