Top Commenters: exclude admin

I’m using the following code in my function.php to display the top commenters in my theme:

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>".get_avatar($result->comment_author_email, 30).' <p>'.(($result->comment_author_url) ? "<a href='".$result->comment_author_url."'>" : "").$result->comment_author.(($result->comment_author_url) ? "</a>" : "")." (".$result->comments_count.")</p></li>";
    }
    $output .= "</ul>";

    echo $output;
}

It works fine, but I’d like to exclude myself, aka the admin/author.
Is this possible at all? If so, how would I do that? In what way would I have to modify the code above?

Thanks a lot in advance! 🙂

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Extend the WHERE clause:

WHERE
    comment_author_email != "" 
    AND comment_author_email != "YOUR_MAIL_ADDRESS" 
    AND comment_type = "" 
    AND comment_approved = 1 
)

If you want to exclude multiple email addresses, use the NOT IN operator and a comma separated list of strings:

AND comment_author_email NOT IN ( "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb838e878784ab8e938a869b878ec5888486">[email protected]</a>", "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="275048554b4367425f464a574b420944484a">[email protected]</a>" )

Method 2

comment_author_email is fine but as far as I’m concerned I prefer using user id :

AND user_id!="1"

It should do the job !


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x