admin管理员组文章数量:1125753
here is the commonly recommended sql command for removing posts revisions and cleaning up the wp database:
DELETE a,b,c
FROM `wp_posts` a
LEFT JOIN `wp_term_relationships` b ON (a.ID = b.object_id)
LEFT JOIN `wp_postmeta` c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision';
how can i modify it to keep let's say the last 3 revisions ?
here is the commonly recommended sql command for removing posts revisions and cleaning up the wp database:
DELETE a,b,c
FROM `wp_posts` a
LEFT JOIN `wp_term_relationships` b ON (a.ID = b.object_id)
LEFT JOIN `wp_postmeta` c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision';
how can i modify it to keep let's say the last 3 revisions ?
Share Improve this question edited Sep 10, 2013 at 14:12 Matoeil asked Sep 10, 2013 at 12:48 MatoeilMatoeil 3211 gold badge6 silver badges14 bronze badges 7 | Show 2 more comments2 Answers
Reset to default 2You can prevent more than three revisions from being saved to the database in the future by adding this line to your wp-config.php
file:
define( 'WP_POST_REVISIONS', 3 );
That line should limit new posts to three revisions, but it won't go through your database and clean it up.
The SQL snippet that you found for deleting old revisions from the WordPress database has problems, which you can read about in this thread on WordPress.org. The thread outlines the problems, and offers alternatives. The thread also reviews some plugins that will do this for you.
Here's an approach to delete all revisions except the last one (I know its not what was asked originally but I hope this helps somebody to use a similar solution).
Step 1. Create temporary table with the revisions that you want to keep:
DROP TABLE IF EXISTS t_keep_revisions;
CREATE TEMPORARY TABLE t_keep_revisions AS (
SELECT
MAX(p.ID) as ID,
p.post_parent
FROM wp_posts p
WHERE p.post_type = 'revision'
GROUP BY p.post_parent
ORDER BY p.post_parent
);
Step 2. Delete all Revision Posts except those which ID is in the table created above
DELETE FROM wp_posts
WHERE post_type = 'revision'
AND ID NOT IN (
SELECT ID
FROM t_keep_revisions
);
Step 3. Delete orphan metadata from wp_postmeta
DELETE FROM wp_postmeta
WHERE post_id NOT IN (
SELECT ID FROM wp_posts
)
本文标签: sqlHow to remove in the wordpress database all posts revisions except the last three
版权声明:本文标题:sql - How to remove in the wordpress database all posts revisions except the last three? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736674949a1947129.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$wpdb
? No. Do you want to know where this is best hooked? No. Do you want to have WPDB Error output? No. – kaiser Commented Sep 10, 2013 at 14:05