Skip to content

Instantly share code, notes, and snippets.

@bueltge
Created November 5, 2012 13:21
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bueltge/4017151 to your computer and use it in GitHub Desktop.
WordPress SE 71248: Delete revisions on post publish
<?php
/**
* Plugin Name: WPSE71248 Delete Revions on Publish Posts
* Plugin URI: http://wordpress.stackexchange.com/questions/71248/
* Description:
* Version: 1.0.0
* Author: Frank Bültge
* Author URI: http://bueltge.de
* License: GPLv3
*/
! defined( 'ABSPATH' ) and exit;
add_action( 'publish_post', 'fb_remove_revisions' );
function fb_remove_revisions( $post_id = FALSE ) {
$post_id = (int) $post_id;
$revisions = '';
// Get the revisions
$revisions = new WP_Query( array(
'post_status' => 'inherit',
'post_type' => 'revision',
'showposts' => -1,
'posts_per_page' => -1,
'post_parent' => $post_id
) );
if ( empty( $revisions ) )
return $post_id;
// Remove the revisions the non-core-way
global $wpdb;
foreach ( $revisions->posts as $revision ) {
$query = $wpdb->prepare(
"
DELETE FROM $wpdb->posts
WHERE ID = %d
AND post_parent = %d
",
$revision->ID,
$post_id
);
$wpdb->query( $query );
}
return $post_id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment