I have this action/hook:

add_action( 'before_delete_post', 'my_delete_function' );
 function my_delete_function($post_id) { 
    global $wpdb;
    $achievement = get_the_category($post_id); 
    $h = $achievement[0]->cat_ID; 
    $s = ''.str_replace('"', '', $h);
   if ( $s == 6 || $s == 5){
        $wpdb->query("DELETE FROM wp_votes WHERE post = ".$post_id) or die(mysql_error());


The above works great when I am deleting posts from the admin. However, when the wp_delete_post() is called, the action doesnt work.

How can I make the action work with the delete function?

2 Answers 2


wp_delete_post() always triggers before_delete_post - the problem must be that you're hooking your function conditionally (i.e. not all the time).

before_delete_post only gets triggered if you're actually deleting the post permanently, not just trashing it.

If you need to listen for trashing too, use wp_trash_post.


Usage wp_delete_post:

wp_delete_post($postid, $force_delete = false)

'before_delete_post' hook is only called when post is force deleted. And not when it's just sent to trash. By default just


sends the post to trash. Because default $force_delete flag is false. So, you have to force delete it by setting the flag to true:

wp_delete_post( $postid, true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.