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
ADVERTISEMENT

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

Hello world!

March 26, 2022

How music shapes the way we play video games

February 26, 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.

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

Hello world!

by admin
March 26, 2022
0

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

How music shapes the way we play video games

by admin
February 26, 2022
0

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated...

Building a Southwest Pacific regional organization

by admin
February 25, 2022
0

Life is about not knowing, having to change, taking the moment and making the best of it, without knowing what's...

The time to unlock Industry 4.0 growth is now

by admin
February 24, 2022
0

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated...

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

Bringing yoga, peace and wellness to your home

February 21, 2022

How big data helping transform public transport system

February 15, 2022

Popular Story

  • clean up metadata for unexist images

    WordPress Database Optimize: Clean up metadata for unexist images

    597 shares
    Share 239 Tweet 149
  • On hover woocommerce product thumbnail change flickity gallery flatsome theme

    596 shares
    Share 238 Tweet 149
  • Can’t Create New Facebook Account: Guide To Solve Some Common Issues

    593 shares
    Share 237 Tweet 148
  • 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.