WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Top Commenters: exclude admin

While back I found this hack here to display top commenters. It works fine. But I would like to exclude the admin from the commenters list. So Please help modify the code to exclude the admin Thanks.

The code is:

function top_comment_authors($amount = 5) { 

    global $wpdb;  
    $results = $wpdb->get_results(' 
            COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url 
            comment_author_email != "" AND comment_type = "" AND comment_approved = 1 
        GROUP BY 
        ORDER BY 
            comments_count DESC, comment_author ASC 
        LIMIT '.$amount  
$output = "<ul>"; 
    foreach($results as $result){ 
        $output .= "<li>".(($result->comment_author_url) ? "<a href='".$result->comment_author_url."'>" : "").$result->comment_author.(($result->comment_author_url) ? "</a>" : "")." (".$result->comments_count.")</li>"; 

    $output .= "</ul>";  
    echo $output;  
share|improve this question

marked as duplicate by EAMann Apr 10 '12 at 20:40

This question was marked as an exact duplicate of an existing question.

found the simple and straightforward answer here: wordpress.stackexchange.com/questions/34251/… – Tara Nov 20 '11 at 17:03

Assuming the comment_author is a User ID, try:

    $output .= "<li>".(($result->comment_author_url) ? "<a href='".$result->comment_author_url."'>" : "").$result->comment_author.(($result->comment_author_url) ? "</a>" : "")." (".$result->comments_count.")</li>";

If not, one can use get_comment, and select the comment_ID in the SQL statement. You can then use the object returned to grab the user ID of the comments author, and do a check to see if they are an administrator using the user_can function from above.



  1. you need to the comment_ID. Modify your SQL query to retrieve this.
  2. inside the foreach loop, call get_comment which will return you a comment object. For an example see here
  3. From this comment object, grab the user ID of the commenter and assign it to a variable
  4. call user_can, passing the user ID you got in step 3. you need to check if the user can change options, which only an administrator can do, so we pass in 'manage_options' as the second parameter
  5. if user_can returns false, list the comment
  6. if user_can returns true, skip to the next comment, this is an administrator comment
share|improve this answer
Thanks for your time and help. I am really lost and don't know how and where to insert your code into my code. I tried different ways but no result. So, please rewrite my code with your mod so that it excludes the admin. Thanks a lot. – Tara Nov 16 '11 at 17:07
Replace what's inside the for loop. You should read up on what I've linked however in order to understand what my code is doing. – Tom J Nowell Nov 16 '11 at 17:16
Also you will need to add comment_ID to the SQL statement SELECT – Tom J Nowell Nov 16 '11 at 17:17
I wish I new what you are talking about. I have noidea. Anyway, I want to thank you for your time and trying to help me. – Tara Nov 16 '11 at 17:21
there is a foreach loop in your code, take what is inside it, delete it and replace it with my code – Tom J Nowell Nov 16 '11 at 17:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.