这是执行此操作的正确挂钩:
add_action(\'before_delete_post\', \'delete_post_attachments\');
function delete_post_attachments($post_id){
global $wpdb;
global $post_type;
if (!$post_type == \'bkroadkill\')
return;
$args = array(
\'post_type\' => \'attachment\',
\'post_status\' => \'any\',
\'posts_per_page\' => -1,
\'post_parent\' => $post_id
);
$attachments = new WP_Query($args);
$attachment_ids = array();
if($attachments->have_posts()) : while($attachments->have_posts()) : $attachments->the_post();
$attachment_ids[] = get_the_id();
endwhile;
endif;
wp_reset_postdata();
if(!empty($attachment_ids)) :
$delete_attachments_query = $wpdb->prepare(\'DELETE FROM %1$s WHERE %1$s.ID IN (%2$s)\', $wpdb->posts, join(\',\', $attachment_ids));
$wpdb->query($delete_attachments_query);
endif;
}