Created
April 25, 2017 21:02
-
-
Save musamamasood/fc74f243818456a1b6bc9ecadd062ad9 to your computer and use it in GitHub Desktop.
Removed attachment for custom post type after plugin deleted.
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
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