How To: Top Commenters (or Commentators) with SQL code
As I mentioned in one of my WDYDT posts, I installed a "Top Commenters" list on this blog. It's only on the homepage, as it's the homepage that passes the most "juice"…
There's a few different plugins that can handle this, but the one I used is called 'Top/Recent Commenters' by Scott Reilly of comment2code.com.
Why did I install it?
- To give a thank-you to more active commenters
- To hopefully entice others to comment on the blog
It remains to be seen whether this plugin will do #2, but for sure it should do #1.
I then went on the hunt for other blogs that have a "top commenters" plugin to see how they've implented it. I noticed that most (if not all) the blogs I found tended to reset the counter on a given day each month.
For me, this is too much like hard work. OK, so it's only a few moments, but I'd most likely forget to do it. It might well be that this is done automatically - I am not sure - anyone know? Perhaps there is a different "top commenters" that can do this?
An extra piece of SQL
I wanted to make a solution that only counts comments made in the last 1 month. So I added a small piece of SQL into the plugin, like this;
$sql .= "AND comment_date > date_sub( NOW( ) , INTERVAL 1 MONTH ) ";
What this code does is say if the comment date is older than 1 month, disregard it. In effect, only comments YOUNGER than 1 month are counted. Hence, I now have an auto-updating "top commenters" plugin.
Going Further
If you had a really popular blog, you could change the 1 month setting to 7 days, or 1 day, or 12 hours, or even 1 hour! It's easy, here's a couple of examples;
$sql .= "AND comment_date > date_sub( NOW( ) , INTERVAL 7 DAY ) ";
and
$sql .= "AND comment_date > date_sub( NOW( ) , INTERVAL 1 HOUR ) ";
I'm sure that you can see the possibilities that having an auto-updating feature like this can bring to your blog. Older comments drop off, making the list of Top Commenters become more "real time".
Enjoy.

Comment by Will — August 10, 2007 @ 5:31 pm
Sounds like unnecessary work to reset stats every month. An easier option would be to use the Show Top Commentators plugin instead!
Comment by Will — August 10, 2007 @ 9:23 pm
I think I already posted a comment on this, but the Show Top Commentators plugin handles the regular reset out of the box.
Comment by Gary — August 11, 2007 @ 11:22 am
Looks like that versin would have saved me writing a line of code
Nice find!