delete_attachment( $attachment_id, true );
wp_delete_post( $attachment_id, true );
是你最好的朋友。您将需要以下内容:
$args = array(
\'post_type\' => \'attachment\',
\'numberposts\' => null,
\'post_status\' => null,
\'post_parent\' => $post->ID,
\'post__not_in\' => array(
get_post_thumbnail_id( $post->ID )
),
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
delete_attachment( $attachment_id, true );
wp_delete_post( $attachment_id, true );
}
}
“true”语句定义从后端和文件系统中完全删除附件及其ID。如果要将它们存储到垃圾桶中,请将其设置为false。