X

How To List Top Commentators in WordPress

Snippets by IsItWP

Are you looking for a way to list the top commentators on your blog? This snippet will let you display the top five commentators using a function.

Instructions:

  1. Add this code to your theme’s functions.php file or in a site-specific plugin.
  2. function top_comment_authors($amount = 5){
    
    	global $wpdb;
    
    	$results = $wpdb->get_results('
    		SELECT
    			COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
    		FROM
    			'.$wpdb->comments.'
    		WHERE
    			comment_author_email != "" AND comment_type = "" AND comment_approved = 1
    		GROUP BY
    			comment_author_email
    		ORDER BY
    			comments_count DESC, comment_author ASC
    		LIMIT '.$amount
    
    	);
    
    	$output = "<ul>";
    	foreach($results as $result){
    		$output .= "<li>".$result->comment_author."</li>";
    	}
    	$output .= "</ul>";
    
    	echo $output;
    
    }
    
  3. Add this function snippet to one your theme’s templates to list the top commentators on your blog.
  4. <?
             top_comment_authors();
    ?>
    

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 9 Best SEO Tools to Grow Your Website Traffic, FAST!

Comments   Leave a Reply

  1. How could I split the list in two?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!