Created
March 2, 2022 13:17
-
-
Save chandrapatel/16a8bf0fb21bf241383a5e61fb7ad68b to your computer and use it in GitHub Desktop.
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 | |
/** | |
* WP-CLI command to delete featured images of given post type's posts. | |
* | |
* Note: First do dry-run, verify log and then do actual run. | |
*/ | |
if ( ! defined('WP_CLI') || ! WP_CLI ) { | |
return; | |
} | |
WP_CLI::add_command( 'featured-media', 'WP_Featured_Media' ); | |
class WP_Featured_Media { | |
/** | |
* Delete featured images of given post type's posts. | |
* | |
* ## OPTIONS | |
* | |
* [--post_type] | |
* : Post type slug | |
* | |
* [--dry-run] | |
* : Whether or not to do dry run | |
* --- | |
* default: true | |
* options: | |
* - true | |
* - false | |
* | |
* ## EXAMPLES | |
* | |
* wp featured-media delete --post_type=product --dry-run=true | |
* wp featured-media delete --post_type=product --dry-run=false | |
* | |
* wp featured-media delete --post_type=product_variation --dry-run=true | |
* wp featured-media delete --post_type=product_variation --dry-run=false | |
* | |
* @subcommand delete | |
* | |
* @param array $args Positional arguments. | |
* @param array $assoc_args Associative arguments. | |
*/ | |
public function delete( $args = [], $assoc_args = [] ) { | |
if ( empty( $assoc_args['post_type'] ) ) { | |
WP_CLI::error( 'Pass post type slug.' ); | |
} | |
$post_type = $assoc_args['post_type']; | |
if ( ! post_type_exists( $post_type ) ) { | |
WP_CLI::error( sprintf( '%s post type does not exist.', $post_type ) ); | |
} | |
if ( isset( $assoc_args['dry-run'] ) && ( true === $assoc_args['dry-run'] || 'true' === $assoc_args['dry-run'] ) ) { | |
$dry_run = true; | |
} else { | |
$dry_run = false; | |
} | |
// Starting time of the script. | |
$start_time = time(); | |
if ( $dry_run ) { | |
WP_CLI::line( sprintf( 'Dry-run: Delete featured media of post type: %s', $post_type ) ); | |
} else { | |
WP_CLI::line( sprintf( 'Delete featured media of post type: %s', $post_type ) ); | |
} | |
$offset = 0; | |
$batch_size = 500; | |
$total_posts = 0; | |
$total_featured_media = 0; | |
$total_failed = 0; | |
$total_success = 0; | |
$total_posts_without_featured_media = 0; | |
$query_args = array( | |
'offset' => $offset, | |
'post_type' => $post_type, | |
'posts_per_page' => $batch_size, | |
'post_status' => 'any', | |
'fields' => 'ids', | |
); | |
$query = new \WP_Query( $query_args ); | |
do { | |
$query->set( 'offset', $offset ); | |
$posts = $query->get_posts(); | |
if ( empty( $posts ) ) { | |
break; | |
} | |
$offset += $batch_size; | |
$count = count( $posts ); | |
$total_posts += $count; | |
foreach ( $posts as $post_id ) { | |
WP_CLI::line( "\n" . 'Deleting featured media of post ID: ' . $post_id ); | |
$thumbnail_id = get_post_thumbnail_id( $post_id ); | |
if ( empty( $thumbnail_id ) ) { | |
WP_CLI::warning( "\n" . 'Featured media not found.' ); | |
$total_posts_without_featured_media++; | |
continue; | |
} | |
WP_CLI::line( "\n" . sprintf( 'Featured media ID: %s.', $thumbnail_id ) ); | |
$total_featured_media++; | |
if ( ! $dry_run ) { | |
$is_deleted = wp_delete_attachment( $thumbnail_id, true ); | |
if ( ! empty( $is_deleted ) ) { | |
WP_CLI::success( 'Featured media deleted.' ); | |
$total_success++; | |
} else { | |
WP_CLI::warning( 'Featured media not deleted.' ); | |
$total_failed++; | |
} | |
} else { | |
WP_CLI::success( 'Featured media will be deleted.' ); | |
$total_success++; | |
} | |
} | |
sleep( 1 ); | |
} while ( $count === $batch_size ); | |
WP_CLI::line( "\nSummary:" ); | |
WP_CLI::line( sprintf( 'Total %s posts found : %d', $post_type, $total_posts ) ); | |
WP_CLI::line( sprintf( 'Total %s posts found without featured media : %d', $post_type, $total_posts_without_featured_media ) ); | |
WP_CLI::line( sprintf( 'Total featured media found: %d.', $total_featured_media ) ); | |
if ( $dry_run ) { | |
WP_CLI::line( sprintf( 'Total featured media will be deleted : %d', $total_success ) ); | |
} else { | |
WP_CLI::line( sprintf( 'Total featured media deleted : %d', $total_success ) ); | |
WP_CLI::line( sprintf( 'Total featured media not deleted : %d', $total_failed ) ); | |
} | |
WP_CLI::line( "\n" . sprintf( 'Total time taken to delete all thumbnails: %s', human_time_diff( $start_time, time() ) ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment