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;  

1 Answer 1


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
  • 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
    Commented Nov 16, 2011 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
    Commented Nov 16, 2011 at 17:16
  • Also you will need to add comment_ID to the SQL statement SELECT
    – Tom J Nowell
    Commented Nov 16, 2011 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
    Commented Nov 16, 2011 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
    Commented Nov 16, 2011 at 17:23

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