Last active
March 5, 2018 15:55
-
-
Save 1naveengiri/8b8afbb902b27fd585b05335daff049e to your computer and use it in GitHub Desktop.
Remove Draft post in WordPress using WP-CLI - Multisite - Single Site
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Remove Draft post | |
* Plugin URI: http://1naveengiri.wordpress.com | |
* Description: plugin to remove all draft post | |
*/ | |
/** | |
* Class to clear old revision data. | |
*/ | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
class Remove_Draft_Command extends WP_CLI_Command { | |
/** | |
* Delete old draft | |
* | |
* | |
* ## OPTIONS | |
* | |
* [<keep>] | |
* : Number of draft to keep per post. Defaults to WP_POST_REVISIONS if it is an integer | |
* | |
* [--post_type=<post-type>] | |
* : Clean draft for given post type(s). Default: any | |
* | |
* [--after-date=<yyyy-mm-dd>] | |
* : Clean draft published on or after this date. Default: none. | |
* | |
* [--before-date=<yyyy-mm-dd>] | |
* : Clean draft published on or before this date. Default: none. | |
* | |
* [--post_id=<post-id>] | |
* : Clean draft for given post. | |
* | |
* [--hard] | |
* : Hard delete. Slower, uses wp_delete_post_revision(). | |
* | |
* [--dry-run] | |
* : Dry run, just a test, no actual cleaning done. | |
* | |
* ## EXAMPLES | |
* | |
* wp draft remove | |
* wp draft remove 5 | |
* wp draft remove 5 --post_type=post,page | |
*/ | |
public function remove( $args = array(), $assoc_args = array() ) { | |
global $wpdb; | |
if ( ! empty( $assoc_args['post_id'] ) ) { | |
$posts = array( $assoc_args['post_id'] ); | |
} else { | |
if ( !empty( $assoc_args['post_type'] ) ) { | |
$post_types = explode( ',', $assoc_args['post_type'] ); | |
} | |
$where = ''; | |
$post_type_where = array(); | |
foreach ( $post_types as $post_type ) { | |
$post_type_where[] = $wpdb->prepare( 'post_type = %s', $post_type ); | |
} | |
$where = ' AND (' . implode( ' OR ', $post_type_where ) . ')'; | |
if ( isset( $assoc_args['after-date'] ) && isset( $assoc_args['before-date'] ) ) { | |
$where .= $wpdb->prepare( ' AND (post_date < %s AND post_date > %s)', $assoc_args['before-date'], $assoc_args['after-date'] ); | |
} else if ( isset( $assoc_args['after-date'] ) ) { | |
$where .= $wpdb->prepare( ' AND post_date > %s', $assoc_args['after-date'] ); | |
} else if ( isset( $assoc_args['before-date'] ) ) { | |
$where .= $wpdb->prepare( ' AND post_date < %s', $assoc_args['before-date'] ); | |
} | |
$where .= $wpdb->prepare( " AND post_status LIKE %s", 'draft'); | |
$query = "SELECT ID FROM $wpdb->posts WHERE 1=1 {$where}"; | |
// get all IDs for posts in given post type(s). | |
$posts = $wpdb->get_col( $query ); | |
} | |
$total = count( $posts ); | |
$notify = \WP_CLI\Utils\make_progress_bar( sprintf( 'Remove draft for %d post(s)', $total ), $total ); | |
if ( isset( $args[0] ) ) { | |
$keep = intval( $args[0] ); | |
} | |
$total_deleted = 0; | |
// error_log(print_r($posts, true)); | |
// WP_CLI::debug($posts); | |
foreach ( $posts as $post_id ) { | |
$total_deleted++; | |
if ( empty( $assoc_args['dry-run'] ) ) { | |
WP_CLI::log( sprintf( 'Removing %d (old draft).', $post_id ) ); | |
wp_delete_post( $post_id, true ); | |
WP_CLI::success( sprintf( 'Successfully removed %d (old draft).', $post_id ) ); | |
} | |
} | |
if ( ! empty( $assoc_args['dry-run'] ) ) { | |
WP_CLI::success( sprintf( 'Dry Run: Will remove %d old draft.', $total_deleted ) ); | |
} else { | |
WP_CLI::success( sprintf( 'Finished removing %d old draft.', $total_deleted ) ); | |
} | |
} | |
/** | |
* List all draft | |
* | |
* ## OPTIONS | |
* | |
* [--post_type=<post-type>] | |
* : List draft for given post type(s). | |
* | |
* [--after-date=<yyyy-mm-dd>] | |
* : Clean draft published on or after this date. Default: none. | |
* | |
* [--before-date=<yyyy-mm-dd>] | |
* : Clean draft published on or before this date. Default: none. | |
* | |
* [--post_id=<post-id>] | |
* : List draft for given post. Trumps --post_type. | |
* | |
* [--yes] | |
* : Answer yes to the confirmation message. | |
* | |
* ## EXAMPLES | |
* | |
* wp draft list | |
* wp draft list --post_id=2 | |
* wp draft list --post_type=post,page | |
* | |
* @subcommand list | |
*/ | |
public function list_( $args = array(), $assoc_args = array() ) { | |
global $wpdb; | |
if ( ! empty( $assoc_args['post_id'] ) ) { | |
$posts = array( $assoc_args['post_id'] ); | |
} else { | |
if ( !empty( $assoc_args['post_type'] ) ) { | |
$post_types = explode( ',', $assoc_args['post_type'] ); | |
} | |
$where = ''; | |
$post_type_where = array(); | |
foreach ( $post_types as $post_type ) { | |
$post_type_where[] = $wpdb->prepare( 'post_type = %s', $post_type ); | |
} | |
$where = ' AND (' . implode( ' OR ', $post_type_where ) . ')'; | |
if ( isset( $assoc_args['after-date'] ) && isset( $assoc_args['before-date'] ) ) { | |
$where .= $wpdb->prepare( ' AND (post_date < %s AND post_date > %s)', $assoc_args['before-date'], $assoc_args['after-date'] ); | |
} else if ( isset( $assoc_args['after-date'] ) ) { | |
$where .= $wpdb->prepare( ' AND post_date > %s', $assoc_args['after-date'] ); | |
} else if ( isset( $assoc_args['before-date'] ) ) { | |
$where .= $wpdb->prepare( ' AND post_date < %s', $assoc_args['before-date'] ); | |
} | |
$where .= $wpdb->prepare( " AND post_status LIKE %s", 'draft'); | |
// post_type = 'draft' | |
$query = "SELECT ID, post_title FROM $wpdb->posts WHERE 1=1 {$where}"; | |
// get all IDs for posts in given post type(s). | |
$posts = $wpdb->get_results( $query ); | |
} | |
$total = count( $posts ); | |
if ( $total > 100 ) { | |
WP_CLI::confirm( sprintf( 'List all %d draft?', $total ), $assoc_args ); | |
} | |
$formatter = new \WP_CLI\Formatter( $assoc_args, array( 'ID', 'post_title' ), 'draft' ); | |
$formatter->display_items( $posts ); | |
WP_CLI::success( sprintf( '%d draft.', $total ) ); | |
} | |
} | |
WP_CLI::add_command( 'draft', 'Remove_Draft_Command' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment