I'm working on site where post except 'featured image' has also other images. Under certain circumstances I need to delete all images except 'featured image'. Which one the most convenient way to do it? Is it possible to do via delete_post_media() function or I should write custom query? Thanks.


1 Answer 1


If someone has clashed with such problem, I solved it in this way:

$attachments = get_posts( array(
    'post_type'      => 'attachment',
    'posts_per_page' => -1,
    'post_status'    => 'any',
    'post_parent'    => $post_id
) );
$thumbId = get_post_thumbnail_id($post_id);
foreach ( $attachments as $attachment ) {
    if($thumbId != $attachment->ID) wp_delete_attachment( $attachment->ID, true );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.