before_delete_postaction-hookWP 3.2.0

Fires before a post is deleted, at the very beginning of wp_delete_post().

The event fires before any actions that remove the post from the database, such as deleting metadata or changing the parent of the deleted post's attachments.

It fires only when a post is deleted from the Trash or when the Trash is disabled, meaning that the post is being permanently deleted.

The hook does not fire when an attachment (Media Library file) is deleted. To perform an action when deleting an attachment, use delete_post, which fires immediately before the post itself is deleted from the database.

The hook also does not fire when a post is moved to the Trash. When the Trash is enabled, it fires only when the post is deleted from the Trash.

Use deleted_post to perform an action after a post, all its relationships, and its metadata have been deleted. Keep in mind that the post no longer exists in the database at that point. Its basic data is available from variables passed to the hook, but not through get_post().

Usage

add_action( 'before_delete_post', 'wp_kama_before_delete_post_action', 10, 2 );

/**
 * Function for `before_delete_post` action-hook.
 * 
 * @param int     $post_id Post ID.
 * @param WP_Post $post    Post object.
 *
 * @return void
 */
function wp_kama_before_delete_post_action( $post_id, $post ){

	// action...
}
$postid(int)
ID of the post passed to the function attached to the event.
$post(WP_Post) (WP 5.5)
Post object.

Examples

#1 Perform an action when deleting a post

Suppose a plugin must perform an action when a custom post type named my_post_type is deleted:

add_action( 'before_delete_post', 'my_func' );
function my_func( $postid ){
	// Check whether our post type is being deleted.

	$post = get_post( $postid );

	// Return if it is not.
	if( ! $post || $post->post_type !== 'my_custom_post_type' )
		return;

	// Code that performs the required action during deletion.
}

#2 Prevent specified users from deleting posts with particular IDs

add_action( 'pre_trash_post', 'restrict_post_deletion', 10, 1 );
add_action( 'pre_delete_post', 'restrict_post_deletion', 10, 1 );

function restrict_post_deletion( $delete, $post ){

	$user = get_current_user_id();

	$restricted_users = [ 21, 25, 54, 2,19 ];
	$restricted_pages = [ 2, 21, 52, 64 ];

	if( in_array( $user, $restricted_users ) && in_array( $post->ID, $restricted_pages ) ){
		wp_die( "You are not authorized to delete this page." )
	}

}

Changelog

Since 3.2.0 Introduced.
Since 5.5.0 Added the $post parameter.

Where the hook is called

wp_delete_post()
before_delete_post
wp-includes/post.php 3887
do_action( 'before_delete_post', $post_id, $post );

Where the hook is used in WordPress

wp-includes/default-filters.php 593
add_action( 'before_delete_post', '_reset_front_page_settings_for_post' );
wp-includes/default-filters.php 594
add_action( 'before_delete_post', '_reset_privacy_policy_page_for_post' );
wp-includes/default-filters.php 814
add_action( 'before_delete_post', '_wp_before_delete_font_face', 10, 2 );