最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

comments - Top Commenters: exclude admin

programmeradmin0浏览0评论

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! :)

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! :)

Share Improve this question edited Nov 14, 2019 at 18:57 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Nov 20, 2011 at 5:17 japanwormjapanworm 5873 gold badges19 silver badges40 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

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 ( "[email protected]", "[email protected]" )

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 !

发布评论

评论列表(0)

  1. 暂无评论