delete_post hook – deleting multiple items

Question

I am using delete_post hook.

add_action('delete_post', array($this, 'remove_audio_file')); // delete post event

(Using it inside the class).

When I delete a single post I have no problem retrieving its ID.

global $post;

However, when I delete multiple posts (2 or more) $post becomes NULL.
Can you advise why is that / what I am doing wrong ?

EDIT:

function remove_audio_file() {
        global $post;
        if ($post != NULL) {
           $unique_id = get_post_meta($post->ID, 'ecpt_unique_id', true);
            $username = get_userdata($post->post_author)->user_login;
            $uploadrr = new bt_uploader();
            $dirPath = $uploadrr->audio_folder() . $username . '/' . $unique_id;
            if (is_dir($dirPath)) {
                foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) {
                    $path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname());
                }
            } 
        }
    }
0
user31412 8 years 2013-04-12T15:28:13-05:00 0 Answers 84 views 0

Leave an answer

Browse
Browse