How to Display Users with the Most Comments in WordPress

Home » Snippets » How to Display Users with the Most Comments in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

If you have a WordPress site that generates a lot of comments, you may want to showcase the user who has contributed the most comments. Displaying the user with the most comments can incentivize other users to engage in discussions and create a sense of community on your site. In this article, we’ll show you how to display the user with the most comments using built-in WordPress functions or a plugin.

					function wpturbo_display_user_most_comments() {
    global $wpdb;
    $query = "SELECT COUNT(comment_author_email) AS comment_count, comment_author_email, comment_author, 
            comment_author_url FROM {$wpdb->comments} WHERE comment_author_email != '' AND comment_approved = 1 
            GROUP BY comment_author_email ORDER BY comment_count DESC LIMIT 1";
    $result = $wpdb->get_results( $query );
    if ( $result ) {
        $author = $result[0]->comment_author;
        $email = $result[0]->comment_author_email;
        $comment_count = $result[0]->comment_count;
        echo '<p>The user with the most comments is ' . $author . ' with ' . $comment_count . ' comments.</p>';
    } else {
        echo '<p>No comments found.</p>';
    }
}
add_shortcode( 'wpturbo_most_comments_user', 'wpturbo_display_user_most_comments' );
				

In this article, we will learn how to display the user who has made the most comments on a WordPress website. This is a perfect use case for using a shortcode to display the information on a post or page.

First, we create a new function called wpturbo_display_user_most_comments(). Inside this function, we use WordPress’s global $wpdb object to create a SQL query that counts the number of comments made by each author, orders this list by the number of comments in descending order and returns the top author with the most comments.

global $wpdb;
$query = "SELECT COUNT(comment_author_email) AS comment_count, comment_author_email, comment_author, 
        comment_author_url FROM {$wpdb->comments} WHERE comment_author_email != '' AND comment_approved = 1 
        GROUP BY comment_author_email ORDER BY comment_count DESC LIMIT 1";
$result = $wpdb->get_results( $query );

We check if the result is not empty, if there are results we extract the data from the first element of $result array, assign this data to the variables $author, $email and $comment_count, and display a message using echo with this data. The message will tell the user who has made the most comments and how many comments they have made.

if ( $result ) {
    $author = $result[0]->comment_author;
    $email = $result[0]->comment_author_email;
    $comment_count = $result[0]->comment_count;
    echo '<p>The user with the most comments is ' . $author . ' with ' . $comment_count . ' comments.</p>';
} else {
    echo '<p>No comments found.</p>';
}

Finally, we add the shortcode that will call this function whenever it is included in a post or page. The shortcode "wpturbo_most_comments_user" will trigger the wpturbo_display_user_most_comments() function. This function will output "The user with the most comments is…".

add_shortcode( 'wpturbo_most_comments_user', 'wpturbo_display_user_most_comments' );

By placing the shortcode [wpturbo_most_comments_user] in a post or page, WordPress will execute the function and display the user with the most comments along with the number of comments they have made.

Register an account to save your snippets or go Pro to get more features.