I need to hook a function only when I delete permanently a post from the database, I’ve tried the ‘before_delete_post’ hook, however, it’s called both when it’s trashed and permanently deleted.
The wp_delete_post() function calls wp_trash_post() and should stop processing, but it looks like the wp_delete_post() is called again after the post is trashed.
I’ve seen this question: WordPress Delete hook with wp_delete_post function?. My need is exactly the oposite of it.
before_delete_post
is not called when a post is only trashed.While
wp_delete_post()
can trash posts (if the post is not trashed and its not being forcibly deleted): it does so by callingwp_trash_post()
and exiting the function prior to the triggering the actionbefore_delete_post
.I’ve tested this, the following function will only ‘die’ when you permanently delete a post, but not when its confined to the trash. The
wp_die
is to demonstrate when the call is being made – I do not recommend using it on a live site.The
wp_delete_post()
function can be found here (WP 3.3.1)The issue was the “Sitewide tags” plugin which was misbehaving the natural WP flow. A function called
sitewide_tags_post_delete()
is hooked on ‘trash_post’ and it’s callingwp_delete_post()
to delete the post on the main blog.Following the @Stephen’s approach, I did this: