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.

I want to perform an action when I delete one of my custom post types, which hook should I use:

wp_trash_mycpt

or

trash_mycpt

My action should run solely when mycpt is in the 'publish', 'draft' or 'future' state and moves towards the 'trash' state. When it is removed from trash itself there is no reason to run the function again.

share|improve this question
add comment

3 Answers

The wp_trash_post might be what you're looking for:

Runs when a post or page is about to be trashed. Action function arguments: post or page ID.

Here's some untested code to get you started:

function my_wp_trash_post ($post_id) {
    $post_type = get_post_type( $post_id );
    $post_status = get_post_status( $post_id );
    if( $post_type == 'mycpt' && in_array($post_status, array('publish','draft','future')) ) {
        // do your stuff
    }
}
add_action('wp_trash_post', 'my_wp_trash_post');
share|improve this answer
    
Doesn't the function wp_trash_post() change the post_status to trash before calling the wp_trash_post hook? –  David Gard Mar 15 '13 at 15:51
    
Nope, after. Just look at the code. –  dalbaeb Mar 15 '13 at 15:59
1  
Good that's been changed then. I remember looking at this a couple of years back and that was not the case! –  David Gard Mar 15 '13 at 16:04
    
@dalbaeb I've been using trash_post hook till now, what's the difference between that and wp_trash_post? –  drtanz Mar 15 '13 at 17:12
1  
The link doesn't mention trash_mycpt anywhere. Ok, so if all is working fine then pray tell, what was the point of your question? –  dalbaeb Mar 16 '13 at 22:20
show 3 more comments

Run your function when post status changes from any of publish, draft or future to trash.

<?php
function my_function() {
    global $post;
    if('my_post_type' == $post->post_type) {
        // do your stuff here
    }
}

add_action('publish_to_trash', 'my_function');
add_action('draft_to_trash',   'my_function');
add_action('future_to_trash',  'my_function');

More info: Post Status Transitions

share|improve this answer
    
+1, did not know this existed. –  David Gard Mar 15 '13 at 16:00
1  
These are useful, but I think wp_trash_post/trash_post will do fine in my case as I don't need this level of control actually. –  drtanz Mar 15 '13 at 17:14
add comment

There is a wp_trash_post action that is called, but the post_status is changed to trash beforehand, meaning you would not be able to check whether the Post was published, etc.

EDIT I stand corrected, the post_status is changed to trash AFTER the action is called.

This should get you started however -

add_action('wp_trash_post', 'my_wp_trash_post')
function my_wp_trash_post(){

    if($post->post_type === 'mycpt') :
        // Do what ever you need to do here
    endif;

}

If it is of any use, there is also an delete_post action. For more information, see the Action Reference for delete_posts.

share|improve this answer
    
Wouldn't it be better to use wp_trash_mycpt rather than checking for the post type within the function? –  drtanz Mar 15 '13 at 17:13
add comment

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.