Problem: I am using cred_delete_post_link to delete posts on the front-end of the site. I would like to delete all the images attached to the post as well, including the featured image and posts added to post content.
Solution: You can use some custom code to delete the attachments:
function delete_post_children($post_id) { global $wpdb; if ( get_post_type( $post_id ) == 'bag' ) { $ids = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_parent = $post_id AND post_type = 'attachment'"); foreach ( $ids as $id ) { wp_delete_attachment($id, true); } if(has_post_thumbnail( $post_id )) { $tn_id = get_post_thumbnail_id( $post_id ); wp_delete_attachment($tn_id, true); } } } add_action('before_delete_post', 'delete_post_children');
Relevant Documentation:
https://developer.wordpress.org/reference/functions/wp_delete_attachment/
https://codex.wordpress.org/Plugin_API/Action_Reference/before_delete_post
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 7 replies, has 2 voices.
Last updated by 5 years, 9 months ago.
Assisted by: Christian Cox.