Hitet logo
  • Home
  • WordPress
    • WordPress Plugins
    • WordPress Themes
  • SEO
  • VPS
  • Hosting
  • Mac OS
  • Windows
  • IOS / iPhone
No Result
View All Result
Get Started
Hitet logo
  • Home
  • WordPress
    • WordPress Plugins
    • WordPress Themes
  • SEO
  • VPS
  • Hosting
  • Mac OS
  • Windows
  • IOS / iPhone
No Result
View All Result
hitet logo
No Result
View All Result
Home Wordpress Wordpress Themes

How to permanently delete attachments (photos and files) when delete a wordpress post

admin by admin
March 28, 2022
in Wordpress, Wordpress Themes
0
Share on FacebookShare on Twitter

I’ve worked with wordpress for many years. Sometimes, i have to delete post for some reasons and i see that wordpress doesn’t delete attachments (such as photos and files).  This will cause wasting our server/hosting resource for unecessary thing.

parmently delete attachments of a wordpress post
permanently delete attachments of a wordpress post

Automatically delete post attachments after delete a wordpress post

This php function will automatically delete all post attachments right after we delete a wordpress post.

You might also like

Top 19 cloud web hosting uy tín nhất 2022

Top 19 cloud web hosting uy tín nhất 2022

August 5, 2022
Top 13 web hosting google uy tín nhất 2022

Top 13 web hosting google uy tín nhất 2022

August 5, 2022
function delete_all_attached_media( $post_id ) {
     if ( get_post_type($post_id) == "post" ) {
          $attachments = get_attached_media( '', $post_id );
          foreach ($attachments as $attachment) {
               wp_delete_attachment( $attachment->ID, 'true' );
          }
     }
}
add_action( 'before_delete_post', 'delete_all_attached_media' );

How to use:

Copy and paste the code to functions.php which is located in your current theme directory. Then try to delete a wordpress post which has some attachments, you will see it works so perfectly.

ADVERTISEMENT

Customize with other wordpress custom post type

The code is so easy to understand. As you can see the line: get_post_type($post_id) == “post”. Just change post to a custom post type that you want the function work.

admin

admin

Related Stories

Top 19 cloud web hosting uy tín nhất 2022

Top 19 cloud web hosting uy tín nhất 2022

by admin
August 5, 2022
0

Tổng hợp và liệt ra những cloud web hosting dưới đây hi vọng sẽ giúp các bạn có thêm những...

Top 13 web hosting google uy tín nhất 2022

Top 13 web hosting google uy tín nhất 2022

by admin
August 5, 2022
0

Tổng hợp và liệt ra những web hosting google dưới đây hi vọng sẽ giúp các bạn có thêm những...

Top 19 cheapest web hosting uy tín nhất 2022

Top 19 cheapest web hosting uy tín nhất 2022

by admin
August 5, 2022
0

Tổng hợp và liệt ra những cheapest web hosting dưới đây hi vọng sẽ giúp các bạn có thêm những...

Top 20 web hosting cheap uy tín nhất 2022

by admin
August 5, 2022
0

Tổng hợp và liệt ra những web hosting cheap dưới đây hi vọng sẽ giúp các bạn có thêm những...

Next Post
clean up metadata for unexist images

Wordpress Database Optimize: Clean up metadata for unexist images

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Top 10 Hipster things to do in Canggu where you can chill & party

February 5, 2022

Top 13 google web hosting uy tín nhất 2022

August 5, 2022

Popular Story

  • clean up metadata for unexist images

    WordPress Database Optimize: Clean up metadata for unexist images

    598 shares
    Share 239 Tweet 150
  • On hover woocommerce product thumbnail change flickity gallery flatsome theme

    598 shares
    Share 239 Tweet 150
  • Can’t Create New Facebook Account: Guide To Solve Some Common Issues

    597 shares
    Share 239 Tweet 149
  • How to permanently delete attachments (photos and files) when delete a wordpress post

    591 shares
    Share 236 Tweet 148
  • Govt distributes free medicines to support self-isolating patients

    586 shares
    Share 234 Tweet 147
Hitet Blog

Hitet Blog is a place to share every thing online free.

  • Buy JNews
  • Support Forum
  • Pre-sale Question
  • Contact Us

© 2022 Hitet.net - Hitet Blog.

No Result
View All Result
  • Home
  • WordPress
    • WordPress Plugins
    • WordPress Themes
  • SEO
  • VPS
  • Hosting
  • Mac OS
  • Windows
  • IOS / iPhone

© 2022 Hitet.net - Hitet Blog.

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.