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());
}
}
}
}
That’s not how that hook is meant to be used:
https://developer.wordpress.org/reference/hooks/delete_post/
The action gives you the post ID and a post object.
So don’t access
$_GET
or the global$post
object, instead use the what the action gives you.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.