Remove Featured Image & All Media Uploaded to the Post

I found a solution of removing featured image related to the post when delete the post from this article but, I want to

I found a solution of removing featured image related to the post when delete the post from this article but, I want to remove all the uploaded media also with post removing. Delete all WP image gallery (included generated thumbnails) that are attached to a post. How can I do this?

I found a solution of removing featured image related to the post when delete the post from this article but, I want to remove all the uploaded media also with post removing. Delete all WP image gallery (included generated thumbnails) that are attached to a post. How can I do this?

Share Improve this question asked Apr 10, 2019 at 13:12 Mohsin AlamMohsin Alam 531 silver badge11 bronze badges 1
  • This is the code:gist.github/mohsinworld/6d3d93f1d3289325d36c2f495bc6d7ae – Mohsin Alam Commented Apr 10, 2019 at 13:22
Add a comment  | 

1 Answer 1

Reset to default 2

You can try using the function get_attached_media() like this:

add_action( 'before_delete_post', 'wps_remove_attachment_with_post', 10 );
function wps_remove_attachment_with_post( $post_id ) {

    /** @var WP_Post[] $images */
    $images = get_attached_media( 'image', $post_id );

    foreach ( $images as $image ) {
        wp_delete_attachment( $image->ID, true );
    }
}

Note that this will permanently delete all the image files related to this post. If those attachments are used somewhere else as well, those links will be broken.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745605409a4635645.html

相关推荐

  • Remove Featured Image & All Media Uploaded to the Post

    I found a solution of removing featured image related to the post when delete the post from this article but, I want to

    9小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信