Skip to content

Instantly share code, notes, and snippets.

@musamamasood
Created April 25, 2017 21:02
Show Gist options
  • Save musamamasood/fc74f243818456a1b6bc9ecadd062ad9 to your computer and use it in GitHub Desktop.
Save musamamasood/fc74f243818456a1b6bc9ecadd062ad9 to your computer and use it in GitHub Desktop.
Removed attachment for custom post type after plugin deleted.
function prefix_uninstall_attachments() {
global $wpdb;
$query = sprintf("
SELECT ID
FROM `%s`
WHERE post_type = '%s'
AND post_status NOT IN ( 'auto-draft', 'inherit' )
", $wpdb->posts, {{custom_post_type}} );
$post_ids = $wpdb->get_col($query);
if (!$post_ids)
return;
$attachments = new WP_Query(array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'any',
'post_parent__in' => $post_ids,
'no_found_rows' => true,
'fields' => 'ids'
));
$attachments = $attachments->posts;
if ($attachments) {
foreach ($attachments as $attachment) {
wp_delete_attachment( $attachment );
}
}
}
prefix_uninstall_attachments();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment