• Resolved Shashi Kumar

    (@mastershas)


    Theme Used- Twenty Fifteen
    Plugin Version- 1.0.11
    Issue – I created an ad and tried to delete the ad permanently. The media attached to the post was not getting deleted. I am afraid if the post expires and post will be deleted but all media attached will remain in library. I don’t know It’s a defect or theme conflict.

    After using the following snippet this is working fine.
    function delete_associated_media($id) {
    //check if product
    if (‘advert’ !== get_post_type($id)) return;

    $media = get_children(array(
    ‘post_parent’ => $id,
    ‘post_type’ => ‘attachment’
    ));

    //if no media associated with
    if(empty($media)) return;

    foreach ($media as $file) {
    //pick what you want to do
    wp_delete_attachment($file->ID);
    }
    }

    //used action hook “before_delete_post”
    add_action(‘before_delete_post’, ‘delete_associated_media’);

Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Images attached to advert post type does not get deleted after the post deletion’ is closed to new replies.