DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

If your blog is private or have lots of registered users, it may be interesting to be able to display the number of comments posted by registered users. This is the purpose of this code.

Simply paste the following code where you’d like the count to be displayed. Re-arrange the code as desired.

global $wpdb;
$where = 'WHERE comment_approved = 1 AND user_id <> 0';
$comment_counts = (array) $wpdb->get_results("
		SELECT user_id, COUNT( * ) AS total
		FROM {$wpdb->comments}
		GROUP BY user_id
	", object);
foreach ( $comment_counts as $count ) {
  $user = get_userdata($count->user_id);
  echo 'User ' . $user->display_name . ' comment count is ' . $count->total . '

Credits: Michael H on WordPress forums.

Leave a Comment

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