WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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