Just for clarity: borrowing from this answer, add the following to your theme’s functions.php
:
function wpse_188427_delete_post_media( $post_id ) {
$attachments = get_posts(
array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'any',
'post_parent' => $post_id,
)
);
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->ID );
}
}
add_action( 'before_delete_post', 'wpse_188427_delete_post_media' );
// Uncomment the following line if you also want to delete media when the post is trashed
// add_action( 'wp_trash_post', 'wpse_188427_delete_post_media' );
Related Posts:
- date/time-picker and file upload (image) fields for custom fields (e.g. with more fields)
- Redirect to another page using contact form 7? [closed]
- How to structure a plugin
- What Features would you Most Like to See Added to WordPress? [closed]
- is_plugin_active function doesn’t exist
- Proper Way to Modify Plugin
- Consuming an external API in WordPress?
- How to edit a wordpress plugin without break its update process
- Looking for a Table of Contents (TOC) plugin for WordPress
- WordPress Plugin Development In MVC Architecture, How?
- How can I import a class privately into a plugin?
- Where should I use get_option in a plugin
- GPL and plugins
- Disable Specific Widgets on Selected Pages of a WordPress Website?
- Create page when plugin is activated
- How can I attach images to a post from the media library?
- Integrating plugins in themes
- How to show custom message once on plugin activation?
- Post source link plugin – small modification
- How to add users roles dropdown in registration in wordpress
- How to extend Media Library (WP 4.4)
- wp_schedule_event / cron_schedules – custom recurrence time not working in Plugin
- How to load a new template page according to a particular URL?
- WordPress network: set themes and plugins for new blog
- Themes VS Plugins [duplicate]
- Failed to invoke other hook from the init hook
- How to edit plugin’s html? [closed]
- Can a plugin add to header/footer/body content?
- Multiple plugins using the same OAuth class issues
- How do I future proof my older WordPress theme?
- How to get Plugin Slugs?
- Advanced Integration – WordPress + YOURLS
- Modifying Footnote Plugin for Descriptive Title Attributes
- Getting media library popup in custom plugin admin page
- Organising the plugins folder in wordpress
- Is it possible to personalize the Media Library?
- force customers to add only single item to card per purchase EDD [closed]
- Preventing BFA in WordPress without using a plugin
- Any way to change the actual filename of an image from media manager?
- How to use get_theme_mod in gutenberg editor wordpress?
- Anonymous Postings
- Disable woocommerce cookies and delete cart data automatically
- Run plugins only on certain pages
- Post category with counts been display like a menu
- Post custom Data value in The Events Calendar plugin [closed]
- Best way to maintain and update 3rd-party WP plugin when developer is unresponsive?
- Calling an attribute from a plugin shortcode
- Why can’t I access my Intranet LDAPS with NADI?
- Setup page on first login on multisite
- Login Customizer doesn’t change the background of the register form
- Integrating WP E-commerce plugin into custom theme
- Custom post types – remove default post supports through empty array?
- When I try to de-activate any plugin it gives error “Warning: call_user_func_array() [function.call-user-func-array]”
- WordPress – source link plugin – how to modify it?
- Minimize and Uglify WordPress Plugin Files
- download button for audio file in post [closed]
- How to create plugin in wordpress [closed]
- I want to filter my products with attributes [closed]
- Action on WordPress Install
- Issue removing Media Editor Tabs — Duplicate Items
- Delete data from custom table when deleting a post
- What is the best way to make a custom page from a plugin?
- Automatic recurring payment using YITH subscription and Dokan Stripe
- Why does “updated_post_meta” did not fire when uploading media other than image?
- How to customize maintenance mode notification in wordpress?
- How to keep plugin (media-sync) running even the tab is closed?
- Hide fields in woocommerce settings
- Elementor: How to design Dropdown Select Box Navigation page like Chegg
- implement custom roles in custom plugin
- How to copy the all WordPress media items to another custom plugin folder?
- Bulk Edit Media Library alt, caption, image names
- How to access a wordpress plugin page or add posts without doing it from the WordPress dashboard?
- Adding LOAD MORE on a page
- Optimise wordpress database using SQL after deleting registered images from Media Folder
- Custom Plugin: How to Include Install Buttons of other 3rd Party Plugins?
- Is it possible to pull all featured images from WP database only?
- How to assign a specific service to a specific provider based on location
- Creating custom “player-objects”
- save short-code in my custom plugin settings problem missed something
- Cron job shedules replace?
- Installation failed, trying to install ANY plugin
- Customize plugin templates
- WordPress delete cookie
- multiple string replace of post’s content for large data
- Widget display in theme
- How to use custom footer template in a site-plugin?
- Multi-site error wordpress after migration
- Media Upload to custom database and Custom Directory
- Customize a widget position
- Why the output of an image gallery plugin is not displayed into a page of my custom theme?
- Problems with installing and deleting plugins
- wp_query for more fields plugin
- Issues with Post 2 Post Plugin
- How can I use multi Media Uploader in a plugin?
- Pronamic Info Window
- From a legal perspective, is it okay to customize a plugin’s code?
- Child Plugin Admin Panel
- Does anyone have the Custom Post Permalinks plugin from John P. Bloch? [closed]
- Take Elementor For Email FIeld Check if user is registered. IF not registered then register on woocommerece
- How to disable plugin capability : “create new category”