如何通过sql语句删除wordpress的垃圾评论信息

作者

前年的时候帮朋友做一个wordpress的博客网站。这个网站也没怎么维护,也就添加了几篇文章,不过网站有排名,每天也有几个流量,都是通过谷歌搜索引擎过来的。有点奇怪,是个中文网站。不过网站的问题是有非常多垃圾评论,接近2万条

刚开始朋友是通过wordpress后台进行删除的,发现工作量比较大。问我有没有快速的删除方法。

通过sql域名删除

-- 删除除通过审核后的所有的评论。
delete from wp_comments where  comment_approved <> '1';

-- 删除扩展的评论信息
delete from wp_commentmeta 
where comment_id 
not in (select comment_ID  from wp_comments);

了解更多 wordpresss是怎么标识一个评论是不是垃圾评论

select  comment_approved, count(*) as n 
from wp_comments 
group by comment_approved ;

+------------------+-------+
| comment_approved | n     |
+------------------+-------+
| 0                | 19332 |
| 1                |     2 |
| spam             |    41 |
| trash            |     1 |
+------------------+-------+

wordpress 通过 comment_approved 来标识。
1. 0 -- 表示待审核
2. 1 -- 表示通过审核
3. spam -- 表示垃圾评论
4. trash -- 表示回收仓

上面的删除比较粗暴一点,这里进行一些更细致的删除,就是添加一些删除的条件。目的是看看有没有正常的评论。首先查看,留言的基本信息。

--  查询一共多少条留言
select count(*) from wp_comments;

-- 查询一共多少条留言 (正常的留言,通过审核)
select count(*) from wp_comments where comment_approved=1;

-- 一共有多少个ip进行留言
select count(distinct  comment_author_IP) from wp_comments; 

查询每个的ip的留言数量

 select  comment_author_IP as ip  , count(*) as `count` 
 from wp_comments 
 group by ip 
 order by count desc 
 limit 10;

统计所有评论次数超过10次的服务器,评论的总数量

select  sum(n) from    
(  select  comment_author_IP as ip  , count(*) as n   
   from wp_comments    
   group by ip    
   having n >= 10  order by n desc
) as  n;
+--------+
| sum(n) |
+--------+
|  14653 |
+--------+

如果一个ip发出的评论次数查过10次,则删除

delete from wp_comments where comment_author_IP in  (
     select ip from(
            select comment_author_IP  as ip
            from wp_comments
            group by ip
            having count(*) >= 10
    ) as ip
);

如果内容含有http,可能是是推广连接,删除。

delete from wp_comments where comment_ID in (
 select comment_ID as id from (
    select comment_ID from wp_comments  where comment_content REGEXP 'http'
 ) as id
) ;

发现评论中没有汉字,删除。

delete from wp_comments where comment_ID in (
 select comment_ID as id from (
    select comment_ID from wp_comments  where comment_content not REGEXP '[\u4e00-\u9fa5]{1,}'
 ) as id
) ;

回复

您的电子邮箱地址不会被公开。