How about this? It adapts an example on the get_posts() function reference page.
function 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 ) {
if ( false === wp_delete_attachment( $attachment->ID ) ) {
// Log failure to delete attachment.
}
}
}
add_action( 'before_delete_post', 'delete_post_media' );
Related Posts:
- Get post embedded image caption
- Insert woocommerce products programmatically with featured image and gallery
- All images on one line for each post
- Determine if page is the Posts Page
- Keyboard shortcut for updating a page or post?
- Apply the_title() filter in post & page title, but not in menu title
- Difference between an archive and a page listing posts
- Disable Attachment Pages Completely
- The next_posts_link() works only with original $wp_query
- Using single.php from plugin folder instead of default template folder
- Find the post an attachment is attached to
- Skipping first 3 posts in wp query
- Post slugs and images cannot have same name?
- How to display the page title/content in the Posts page?
- Callback for wp.autosave.server.triggerSave();
- How can I see a list of pages and post where my custom Gutenberg block is used?
- Use an attachment in multiple posts
- RSS for Pages Instead of Posts?
- Download external images if post is publish
- Display posts by tag on page
- Set number of article per number of page
- Pagination for Pages and Posts
- Can I change a post to a page by changing it’s type?
- Overview with latest edited posts and pages
- Add a custom option to a page in backend
- Creating new page with pre-defined parent page
- Why does `url_to_postid` return 0 when testing `page_for_posts` Page?
- Populating a page with content from post custom fields
- Edit menu item title from edit page/post
- Changing my permalink structure – will new layout conflict with existing page?
- disable column on post and user list
- Can a page_id and a post_id be same?
- How can I display widget in a Page or Post?
- Differentiate between posts and pages in search results
- Getting a 404 error when clicking edit page
- Disable most recent & view all (TABS) on nav-menu.php
- Why content_arr[‘extended’] removes paragraph tags?
- How can I schedule a PAGE to go live at a future date/time?
- How to insert 2 args into 1 Wp_Query for a slideshow
- Get attachments for posts that belongs to a specific category
- Get Page ID of blog homepage
- Get Image tag from content of post
- Second comments section for one post/page
- How to create media library for each user?
- 404 error on default post type and default taxonomy fronted page
- When open add new post or page WordPress add post with ID=0 continuously [closed]
- How do I do a page break?
- Automatically create a new page daily with last three posts
- When changing pages to posts, how do you set up 301 redirects for the page URLs?
- 2 dynamic sidebars registered, not showing up
- How to make default “Privacy Policy Page” a normal page?
- static landing page leading to author specific pages w/ “live” content
- Total number of posts in query (category/tag/author/search results/main page…)
- Upload attachment from external site
- WordPress 302 Redirect to Random Product/Post/Page
- New Posts and Pages Won’t Save
- How to get the next and previous image title from attachment or gallery?
- Changing wp_link_pages() to “Next Page” and “Previous Page” buttons?
- Direct some posts to only appear on a specific page
- Adding if statement into the_content()
- Different post views for different category views
- Changing permalink of WordPress
- Creating a redirect option for current page url
- Indenting within a blockquote
- Create a List of Widgets, where each widget holds three attributes
- Change published date and time using SQL query for page in every 5 hour
- Is there a way to know when a page has been updated and do some action only once?
- How do I assign an attachment to a post using only the REST API?
- WordPress Sticky Post Count “Fix” Breaking Pagecount by 72 pages!
- Put page on Archived statut after end of publication date
- How to add a block to a category page?
- post and page and custom post type
- Using WP page password protection as defacto login
- calling pages instead of posts
- WordPress display page information rather than post
- Post Category link is same with Page link
- Add in the url attachment loop the wp_link_pages of the post
- Is it possible to query a category with specified posts?
- How do I set content in my pages so that it remains permanent while blog posts are added below?
- Permalink base only for posts
- Precedence of page permalinks over woocommerce product category links?
- WordPress URL question
- Theme does not respect spaces between paragraphs in pages
- Page and Posts loading as index page? Not loading the content
- My blog “page” used to show a list of posts, but now it’s just showing the page content?
- Converting Posts to Pages
- 404 error on every post and page other than home
- get next/previous post name
- Get post title of faulty link on 404 error page
- Get current post’s child page?
- The next_posts_link() works only with original $wp_query
- Post Attachment missing head (stylesheets/js/what-not)
- Limit amount of pages shown in pagination within wp_query
- how to show all type of author posts in author page (SOLVED)
- custom post type single page template not working
- How to make posts appear under pages
- WordPress Page Column Problem [closed]
- What function does the loop of displaying posts?
- What is the proper use of guid for images in wp_posts?
- Edit Page button is gone