Hook on trash post

The wp_trash_post hook might be what you’re looking for:

Fires before a post is sent to the trash.

Also, there’s the trashed_post hook:

Fires after a post is sent to the trash.

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' );

Leave a Comment