Is it possible to delete post/page revisions from the database when a post/page is published?
11/05/12 Answer: See the plugin below by bueltge
I want to do this on a site with 5,000 posts and 125,000 comments; it’s on a VPS and can handle the wp_posts table size – before I deleted all revisions, the table was 1.5 gigs – but I still want to keep the overall database size to a minimum by preventing revisions.
But, the client tells me he lost a big post due to a browser freeze, so: is it possible to delete post/page revisions from the database for each post/page when it is published?
What hook would be used? Is save_post
the correct one? http://codex.wordpress.org/Plugin_API/Action_Reference/save_post
I have the autosave parameter set in wp-config.php, but according to the docs, that autosaves the post/page as it is being worked on, and overwrites each autosave.
What I want to do is have revisions accumulate for drafts – however many, depending on how many times “Save” and “Update” are used by the author – but once the draft is published, delete all revisions. There will only be revisions for the one post, but rather than try and parse the post_ID, the SQL query can run through all posts.
This is what I’m trying in functions.php, but I get a Call to a member function query() on a non-object
error.
function delete_revisions_on_publish( $post_id ) {
if ( !wp_is_post_revision( $post_id ) ) {
remove_action('save_post', 'delete_revisions_on_publish');
$wpdb->query(
$wpdb->prepare(
"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' "
)
);
}
}
add_action( 'save_post', 'delete_revisions_on_publish' );
I think a small plugin with the hook ‘publish_posts’ is enough. But I dont know about a core function to delete revisions and I use a query with WP functions. The source is untested, written only for this post.
Alternative use the download from Gist 4017151
I would prefer a different approach to achieve that. Rather than using the global
$wpdb
to directly access DB and delete revisions, I have used WP functions. The following code will delete revisions onPublish
orUpdate
of all post types that supportsrevisions
and have apublish
status:It should be possible and it looks like you are on the right track.
To solve your “Call to a member function” error, I believe you need
global $wpdb;
at the top of that function.I think the hook formerly know as
publish_post
would be a better hook for you as it is specific to published posts.save_post
will run for all saves.Also, you say that “the SQL query can run through all posts” but that will erase revisions for all drafts if you have more than one post in the works, or if you save an edit to a published post while you have a draft in the works. Anyway, that is your choice, but you don’t need
$wpdb->prepare
at all if you do that. There is no input other than the hard coded SQL string so there is no need to ‘prepare’ anything.