Just before other data associated with the post is deleted – like meta data. After the other data is deleted, the delete_post action executes, the primary post is removed, and then the deleted_post action is executed.
When items marked for trash are going through final (forced) deletion (e.g. the trash is disabled and an item is deleted).
This hook does not run:
For posts that are being marked as trash (unforced). The wp_trash_post hook is the functional equivalent, executed just before items are marked as trash.
For attachments. The delete_attachment hook is the functional equivalent, executed just before posts of type attachment are processed. With attachments, exactly as with other post types, meta data is removed, the delete_post action is run, then the attachment is removed from the database, and finally, the deleted_post action is run.
<?php
add_action( 'before_delete_post', 'wpdocs_my_func', 99, 2 );
function wpdocs_my_func( $postid, $post ) {
if ( 'wpdocs_my_custom_post_type' !== $post->post_type ) {
return;
}
// My custom stuff for deleting my custom post type here
}
?>
Let’s suppose you have a plugin and when a certain post_type is deleted you want to perform some action.
<?php
add_action( 'before_delete_post', 'wpdocs_my_func' );
function wpdocs_my_func( $postid ) {
// We check if the global post type isn't ours and just return
global $post_type;
if ( 'wpdocs_my_custom_post_type' !== $post_type ) {
return;
}
// My custom stuff for deleting my custom post type here
}
?>
It would be a good practice not to use global variables when the needed information is available in parameters because global variables can easily be broken by different context, plugins or updates.
Let’s suppose you have a plugin and when a certain post_type is deleted you want to perform some action.