Of course your custom posts can’t be trashed. They can’t even get set to private or draft or anything else then publish
because your callback change_content()
always sets the post status to publish
every time a post (of your custom post type) gets saved. However trashing a post just means setting its status to trash
.
I’m not sure what you want to achieve with this line of code:
$my_post['post_status'] = 'publish';
but if you want to track status changes you will have to hook into the transition_post_status
hook. Here’s a quick, yet untested example on how to do this:
namespace WPSE186226;
class TrackPostStatus {
private $tashedPostsIds = [];
/**
* @wp-hook transition_post_status
* @param string $newStatus
* @param string $oldStatus
* @param \WP_Post $post
*/
public function post_status_transition( $newStatus, $oldStatus, \WP_Post $post ) {
//e.g. register when a post gets trashed:
if ( 'trash' !== $old_status && 'trash' === $new_status )
$this->trashedPostsIds[] = $post->ID;
}
/**
* @wp-hook save_post
* @param int $postId
*/
public function handle_post( $postId ) {
if ( ! in_array( $postId, $this->trashedPostIds ) )
return;
// call your logic here.
}
}
$postStatusTracker = new TrackPostStatus;
add_action( 'transition_post_status', [ $postStatusTracker, 'post_status_transition' ], 10, 3);
add_action( 'save_post', [ $postStatusTracker, 'handle_post' ] );
Note: this example uses PHP 5.4 syntax and is not compatible to PHP 5.3 or below
This way provides you a more flexible handling with post status transitions. However there’s a simpler hook when you just want to track the new status: {$new_status}_{$post_type}