[wp-trac] [WordPress Trac] #12108: cant delete attachments when deleting a post

WordPress Trac wp-trac at lists.automattic.com
Mon Feb 1 12:00:27 UTC 2010


#12108: cant delete attachments when deleting a post
-----------------------------+----------------------------------------------
 Reporter:  unsalkorkmaz     |        Owner:         
     Type:  feature request  |       Status:  closed 
 Priority:  normal           |    Milestone:         
Component:  General          |      Version:  2.9.1  
 Severity:  normal           |   Resolution:  wontfix
 Keywords:                   |  
-----------------------------+----------------------------------------------
Changes (by scribu):

  * status:  new => closed
  * resolution:  => wontfix
  * milestone:  2.9.2 =>


Comment:

 An attachment can be used in other posts, without being directly attached
 to them.

 Here's some code I use that does what you want:

 {{{
 function delete_post_children($post_id) {
         global $wpdb;

         $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);
 }
 add_action('delete_post', 'delete_post_children');
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/12108#comment:1>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list