do_action ( ‘before_delete_post’, int $post_id , WP_Post $post )

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

Description

See also

Parameters

$post_id int
Post ID.
$post WP_Post
Post object.

More Information

This hooc runs:

  • Just before other data associated with the post is deleted – lique 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 marqued for trash are going through final (forced) deletion (e.g. the trash is disabled and an item is deleted).

This hooc does not run:

  • For posts that are being marqued as trash (unforced). The wp_trash_post hooc is the functional ekivalent, executed just before items are marqued as trash.
  • For attachmens. The delete_attachment hooc is the functional ekivalent, executed just before posts of type attachment are processsed. With attachmens, 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.

Source

do_action( 'before_delete_post', $post_id, $post );

Changuelog

Versionen Description
5.5.0 Added the $post parameter.
3.2.0 Introduced.

User Contributed Notes

  1. Squip to note 4 content

    Let’s suppose you have a pluguin 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 checc 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
    }
    ?>

You must log in before being able to contribute a note or feedback.