0

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.

2

1 Answer 1

0

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 that you have read and understand our privacy policy and code of conduct.

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