Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tamimibrahim17/64f91d676ce215a11da3af4002b8a93a to your computer and use it in GitHub Desktop.
Save tamimibrahim17/64f91d676ce215a11da3af4002b8a93a to your computer and use it in GitHub Desktop.
WP - Multisite delete revisions
<pre><?php
// WordPress Multisite - Delete all revisions from all posts in a netword.
// Quick hack by @mrazzari, 2014.
// For context see this thread started by Kitchin at the forums:
// http://wordpress.org/support/topic/deleting-post-revisions-do-not-use-the-abc-join-code-you-see-everywhere
// HOWTO
// This snippet is meant to be called as a standalone script.
// Like http://example.com/tmp/multisite_delete_revisions.php
// Note the path to wp-config below, you may need to change it.
include('../wp-config.php');
// Give this script 5 minutes to run.
set_time_limit(60*5);
// Get list of all blogs in our network.
// You can set the below params to (0, 1) to test on a single blog.
$blog_list = get_blog_list_full(0, 'all');
// Delete revisions for each blog.
foreach ($blog_list AS $blog) {
echo "Deleted from " . $blog['domain'] . ': ';
echo delete_revisions_for_blog($blog['blog_id']) . ' revisions.<br>';
flush();
}
echo '<br>DONE.<br>';
/* Delete all revisions for posts in a blog. */
function delete_revisions_for_blog($blog_id){
global $wpdb;
if (is_multisite()){
switch_to_blog( $blog_id );
}
$revision_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_type = 'revision' AND post_name NOT LIKE '%-autosave%'");
foreach ($revision_ids as $revision_id){
// This will invoke wp_delete_post, which takes care of related meta, taxonomy terms, etc.
wp_delete_post_revision($revision_id);
}
// Placing this function inside a plugin?
// You'll want to un-comment the following to avoid breaking your site!
// if (is_multisite()){
// restore_current_blog();
// }
return count($revision_ids);
}
/* Based on WP's own get_blog_list() from wp-includes/ms-deprecated.php,
but also including non-public and archived blogs, and with a stub
to make this work on non-multisite installs as well. */
function get_blog_list_full($start=0, $num=10, $deprecated='') {
global $wpdb, $blog_id;
// For single blogs
if (!is_multisite()){
return array($blog_id => array(
'blog_id' => $blog_id,
'domain' => preg_replace('%https?://%i', '', get_bloginfo('url', 'raw'))
));
}
// For MultiSite setups
$blogs = $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain
FROM $wpdb->blogs WHERE site_id = %d AND deleted = '0' ORDER BY registered ASC", $wpdb->siteid), ARRAY_A );
foreach ((array) $blogs as $details) {
$blog_list[$details['blog_id']] = $details;
}
if (false == is_array($blog_list)){
return array();
}
if ($num == 'all'){
return array_slice($blog_list, $start, count($blog_list));
} else {
return array_slice($blog_list, $start, $num);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment