Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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. . .

share|improve this question
1  
Write the solution below and mark it as answer –  Ashfame Feb 28 '11 at 10:59
    
its not the proper answer as the double execution of function is still there... –  Harjeet Singh Feb 28 '11 at 11:01

2 Answers 2

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

share|improve this answer

That action will be called once when the post is moved to the trash, and then again when the trash is emptied.

Your function could check for the presence of an option with the post ID in the name, if the option is not there then it sends the email and then sets the option. If the option is there (e.g. it's being called a second time) you would not send the email.

Remember then to delete the option, so your database doesn't fill up with them!

add_action( 'delete_post', 'emailUser' );

function emailUser( $postid ) {

   if ( ! get_option( 'user_emailed_' . $postid ) ) {
      // send email here
      update_option( 'user_emailed_' . $postid, 'anything' );
   } else {
      delete_option( 'user_emailed_' . $postid );
   }

}

Let me know if that works!

share|improve this answer
    
you can use the Transients API instead of options so you won't need to worry about deleting options from the database codex.wordpress.org/Transients_API –  Bainternet Feb 28 '11 at 13:25
    
Thanks Simon that works great . . . . dont't know how n why i missed this . . .. –  Harjeet Singh Mar 1 '11 at 5:43
    
Bainternet, i saw the transients and they are pretty similar to get_option in my case and i wud stick with get_option for the delete thing too as the execution would depend on the server performance too and calculation of the code execution time before and after the deletion of post might get difficult. –  Harjeet Singh Mar 1 '11 at 5:57
    
There's no need at all to start adding/editing option fields or transients, adding extra queries. Instead just call did_action to check if the hook has fired already. –  Tom J Nowell Mar 5 '12 at 10:50

Your Answer

 
discard

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.