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.
The
wp_trash_post
hook might be what you’re looking for:Also, there’s the
trashed_post
hook:Here’s some untested code to get you started:
Run your function when post status changes from any of
publish
,draft
orfuture
totrash
.More info: Post Status Transitions
There is a
wp_trash_post
action that is called, but thepost_status
is changed totrash
beforehand, meaning you would not be able to check whether the Post was published, etc.EDIT I stand corrected, the
post_status
is changed totrash
AFTER the action is called.This should get you started however –
If it is of any use, there is also an
delete_post
action. For more information, see the Action Reference fordelete_posts
.