WordPress使用久了,数据库里面有很多没用的东西,下面分享一个删除多余数据的代码
<?php //wordpress数据库多余数据删除 $hostname_blog = "localhost";//填写数据库主机,同wp-config.php $database_blog = "user";//填写数据库名,同wp-config.php $username_blog = "user";//填写数据库用户名,同wp-config.php $password_blog = "password";//设定数据库密码,同wp-config.php $prefix_blog = "wp_";//wordpress的表前缀,同wp-config.php $blog = mysqli_connect($hostname_blog, $username_blog, $password_blog) or trigger_error(mysqli_error(),E_USER_ERROR); mysqli_select_db($blog, $database_blog); mysqli_query($blog, 'DELETE FROM '.$prefix_blog.'_posts WHERE post_type = "revision"'); mysqli_query($blog, 'DELETE FROM '.$prefix_blog.'_postmeta WHERE meta_key = "_edit_lock"'); mysqli_query($blog, 'DELETE FROM '.$prefix_blog.'_postmeta WHERE meta_key = "_edit_last"'); mysqli_query($blog, 'DELETE FROM '.$prefix_blog.'_commentmeta WHERE meta_key LIKE "%trash%"'); mysqli_query($blog, 'DELETE FROM '.$prefix_blog.'_comments WHERE comment_approved = "trash"'); mysqli_query($blog, 'DELETE FROM '.$prefix_blog.'_options WHERE option_name REGEXP "_transient_"'); mysqli_query($blog, 'DELETE FROM '.$prefix_blog.'_postmeta WHERE meta_key = "_wp_attached_file"'); mysqli_query($blog, 'DELETE FROM '.$prefix_blog.'_postmeta WHERE meta_key = "_wp_attachment_metadata"'); mysqli_query($blog, "DELETE FROM ".$prefix_blog."_posts where (post_status='auto-draft' or post_status='inherit') and post_type='post'"); $tablelist = mysqli_query($blog, "SHOW TABLES"); while($checklist = mysqli_fetch_array($tablelist)) { $optimization=mysqli_query($blog, "OPTIMIZE TABLE `$checklist[0]`"); } echo 'WordPress数据库清理完成!';
将上面的代码然后保存为php文件,上传到网站的目录,直接用浏览器打开这个文件就可以了。
或者使用下面的语句,删除自己想删除的内容
DELETE FROM wp_posts WHERE post_type = "revision"; DELETE FROM wp_postmeta WHERE meta_key = "_edit_lock"; DELETE FROM wp_postmeta WHERE meta_key = "_edit_last"; DELETE FROM wp_commentmeta WHERE meta_key LIKE "%trash%"; DELETE FROM wp_comments WHERE comment_approved = "trash"; DELETE FROM wp_options WHERE option_name REGEXP "_transient_"; DELETE FROM wp_postmeta WHERE meta_key = "_wp_attached_file"; DELETE FROM wp_postmeta WHERE meta_key = "_wp_attachment_metadata"; DELETE FROM wp_posts where (post_status='auto-draft' or post_status='inherit') and post_type='post';
执行代码前请确认你的wordpress数据表前缀是否是wp_,如果不是记得改一下代码。
直接登录phpmyadmin中运行sql语句就OK了,执行sql语句前请备份数据库以防万一。
文章参考自:https://www.77nn.net/297.html,修正其中不正确代码,并使用mysqli替换不安全的mysql。
本站文章除注明转载/出处外,均为原创,若要转载请务必注明出处。转载后请将转载链接通过邮件告知我站,谢谢合作。本站邮箱:admin@only4.work
尊重他人劳动成果,共创和谐网络环境。点击版权声明查看本站相关条款。