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.
-
delete_post_media() is not a core function I guess, wordpress.org/support/topic/delete-images-attached-to-page-post, you have to write a custom one, the above link might be of help– KumarJun 17, 2014 at 4:27
-
Yes, of course, I've gone in wrong direction......ok, thanks– Avag SargsyanJun 17, 2014 at 7:44
1 Answer
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 );
}