I have this code
add_action( 'delete_post', 'my_delete_function' );
function my_delete_function() {
global $wpdb;
$wpdb->query("
DELETE FROM wp_votes WHERE post=".$thePostID."
;);
}
How can I get the id of the post being deleted?
Additionally, will this still work if multiple posts are deleted in the admin?
I haven’t tested so I’m providing you with two possibilities, Inside a loop, use the following:
Outside a loop, use the following:
Or:
Or you can pass the post ID in a function, much like
Add a parameter to your function, which will be passed the ID of the current post being deleted.
Using any external variable (like a global in @Asko’s answer) removes any guarantee that the post actually being deleted is the one you’re working on within your function – it could either fail completely, or even delete data for the wrong post!