delete_post hook – deleting multiple items

I am using delete_post hook.

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

(Using it inside the class).

Read More

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());
                }
            } 
        }
    }

Related posts

Leave a Reply

2 comments

  1. When deleting multiple items, $_GET[‘post’] becomes an array with numeric keys starting from 0. To access current item that is being deleted, use global $post_del. This is identical to global $post so all methods and procedures would work the same way. This is not documented on WP Codex. I have found it out myself via debugging.