WordPress自带的自动保存草稿和版本历史功能,用来防止数据库自动添加无用数据。但是如果之前保存的数据还在呢?一次删除一项,显然不合理,也不方便。一些不懂数据库的童鞋可能会被错误删除,所以在这里,整理一下wordpress的sql语句,删除多余的数据。
直接上代码:
//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
$blog = mysql_pconnect($hostname_blog, $username_blog, $password_blog) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_blog, $blog);
mysql_query('DELETE FROM wp_posts WHERE post_type = "revision"');
mysql_query('DELETE FROM wp_postmeta WHERE meta_key = "_edit_lock"');
mysql_query('DELETE FROM wp_postmeta WHERE meta_key = "_edit_last"');
mysql_query('DELETE FROM wp_commentmeta WHERE meta_key LIKE "%trash%"');
mysql_query('DELETE FROM wp_comments WHERE comment_approved = "trash"');
mysql_query('DELETE FROM wp_options WHERE option_name REGEXP "_transient_"');
mysql_query('DELETE FROM wp_postmeta WHERE meta_key = ‘_wp_attached_file’');
mysql_query('DELETE FROM wp_postmeta WHERE meta_key = ‘_wp_attachment_metadata’');
mysql_query("delete from wp_posts where (post_status='auto-draft' or post_status='inherit') and post_type='post'");
$tablelist = mysql_query("SHOW TABLES");
while($checklist = mysql_fetch_array($tablelist)) {
$optimization=mysql_query("OPTIMIZE TABLE `$checklist[0]`");
}echo '清理完成!';
?>
在上面的代码第一句添加< ?php然后保存为
clean.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'
直接登录phpmyadmin中运行sql语句就OK了,执行sql语句前请备份数据库以防万一。