WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Plugin API/Action Reference/before delete post

Contents

Description

before_delete_post is fired before post metadata is deleted.

Properties

$postid
The post id that is being deleted.

Related

wp_delete_post()

Example

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''my_func' );
function 
my_func$postid ){

    
// We check if the global post type isn't ours and just return
    
global $post_type;   
    if ( 
$post_type != 'my_custom_post_type' ) return;

    
// My custom stuff for deleting my custom post type here
}
?>

Note

It's important to note the hook runs only when the WordPress user empties the Trash. If you using this hook note that it will not fire if the user is deleting an Attachment, since attachments are force deleted, i.e., not sent to the Trash. Instead use the delete_post() hook.

Source File

before_delete_post() is located in wp-includes/post.php.