• Resolved FTLRalph

    (@ftlralph)


    Similar to forums showing the user’s number of posts/comments, I’d like to do the same with the users on my website.

    I know I can do this with a COUNT of the database with a user ID…but doing that every time I want to display the user’s comments (under their avatar or wherever) seems like extreme inefficiency.

    I’ve looked countless times, but there doesn’t seem to be a plugin that simply stores the count of a user’s comments. When they make one, increment the value in the DB by 1, if deleted, decrement by 1. To fetch value, simply lookup the user_id in the table rather than count thousands and thousands of rows.

    Am I thinking about it wrong? Any plugins which do this? Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter FTLRalph

    (@ftlralph)

    Thinking about just running a count query on every user as is suggested as most answers to this question:

    <?
    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';
    }
    ?>

    BUT just once a day at night with a cron job or something. It just seems very intense code to be running constantly throughout the day, I’d rather store the value and run it once per day.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    The comment_author_email column is a key field. Therefore it is indexed.

    Just running the query is faster than running it on the regular and storing that information for later retrieval.

    Trust the database cache. It’s better at this than you are.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Actually *store* comment count?’ is closed to new replies.