0

As a way to "reward" users, I'd like to showcase their comment count on their comments.

However, every answer I've seen as to how to achieve this involves querying the database and counting through every comment...which seems overkill and on a site with many users and thousands and thousands of comments - not good.

I don't think there's a plugin?

My approach would be to run the query as suggested in many others' answers once a night in a cron job, and store the values in a DB table with the user id as the key.

Then in the comments on the front end, just lookup the user id and show the value.

Sure it doesn't update in real-time but that's fine I think.

Is this approach also overkill or?...

Something like this would be run once a day for every registered user and value stored in the table:

<?
function commentCount() {
    global $wpdb;
    $count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE comment_author_email = "' . get_comment_author_email() . '"');
    echo $count . ' comments';
}
?>
0

Depends on how many users you have, but it's probably fine.

You could also just keep a running count of comments made by registered user, and add when they comment / subtract when a comment of them gets deleted or rejected / marked as spam.

  • thanks, that was actually my first idea but then thought it may get a bit messy removing/trashing/etc comments, but I might go this route instead since there are the hooks needed – FTLRalph Jan 7 '18 at 19:02

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.