Delete Associated Media Upon Page Deletion

  • Dragoescu
    Dragoescu
    Senior Member 196 points
    September 18, 2015 at 5:20 am

    Hello,

    How the title said. I have a realy problem with my host space because of the associated media.
    Actualy i have 1.800 listing, i have remove another 400-500 listings, but after i delete the items the space host it’s the same.

    I have added on the function.php file the next code but not work:

    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.
    }
    }
    }

  • Dragoescu
    Dragoescu
    Senior Member 196 points
    September 20, 2015 at 6:58 pm

    Hello,

    Please let me know if there it’s any solution for that ?!

    Many regards.

  • Clive
    Clive
    Super Guru 1,818 points
    September 20, 2015 at 8:19 pm

    This is the just way WordPress works. If you delete a post, you need to delete any associated images etc separately.

  • Dragoescu
    Dragoescu
    Senior Member 196 points
    September 21, 2015 at 5:21 am

    So, if i understend, it’s not posible with this code to remove the attach automat when a listing expire right ?!

    Many refagards.

  • Clive
    Clive
    Super Guru 1,818 points
    September 21, 2015 at 10:49 am

    Nope.

  • Dragoescu
    Dragoescu
    Senior Member 196 points
    September 21, 2015 at 11:04 am

    And did you know any solution for that ?!

  • Aaron
    Aaron
    Super Guru 920 points
    September 21, 2015 at 11:59 am

    That code is almost there though its not meant for custom post types, you are probably better off using a plugin like https://wordpress.org/plugins/dx-delete-attached-media/

  • Dragoescu
    Dragoescu
    Senior Member 196 points
    September 21, 2015 at 3:37 pm

    Hello,

    OK, i just instaled. Il will delete some items and i will let you know.

  • Dragoescu
    Dragoescu
    Senior Member 196 points
    September 21, 2015 at 3:47 pm

    Hello,

    i Just remove 30 listing but no change for the space from disk.
    so, i belive that dosen’t work.

Viewing 9 posts - 1 through 9 (of 9 total)

New Business Themes for WordPress 2020

Start your new website today!

View Business Themes