Function to execute when a post is moved to trash .

Hi is there an action which can be performed while a post is moved to trash …. I tried delete_post and deleted_post. It works but twice for each action ( before and after the action as written in codex ) and the output is shown only when the post is deleted from trash. Example i want to send an email to the author stating that his post has been moved to trash because of some reason, and update some user_meta. if i use

add_action('deleted_post', 'emailUser');

or

add_action('delete_post', 'emailUser');

it works only when the post is deleted from trash . . .

Update : got it to work for trash posts using

add_action('trash_post', 'emailUser');

but the problem for double execution of function is still there. . .

3 s
3

This will do the trick!

add_action('trash_post','my_trash_post_function',1,1);
function my_trash_post_function($post_id){
    if(!did_action('trash_post')){
        // do stuff
    }
}

Here we add the function, and to prevent the hook from executing more than once using did_action:

http://codex.wordpress.org/Function_Reference/did_action

As always, these kinds of hooks take the form {post_status}_{post_type}, so trash_post, trash_page, trash_customposttype, publish_page etc

Leave a Comment