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

Fires immediately before a post is deleted from the database.

Parameters

$post_idint
Post ID.
$postWP_Post
Post object.

More Information

Take note, by the time the hook triggers, the post comments and metadata would have already been deleted. Use the before_delete_post hook to catch post deletion before that.

Source

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

Changelog

VersionDescription
5.5.0Added the $post parameter.
1.2.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Let’s suppose you have a plugin that, for one reason or another, stores its own post metadata in a separate database table called codex_postmeta. One of the ways you can achieve synchronization is to be made aware when a post is deleted so that you can replicate the changes yourself.

    add_action( 'admin_init', 'wpdocs_codex_init' ); function wpdocs_codex_init() { add_action( 'delete_post', 'wpdocs_codex_sync', 10 ); } function wpdocs_codex_sync( $pid ) { global $wpdb; $query = $wpdb->prepare( 'SELECT post_id FROM codex_postmeta WHERE post_id = %d', $pid ); $var = $wpdb->get_var( $query ); if ( $var ) { $query2 = $wpdb->prepare( 'DELETE FROM codex_postmeta WHERE post_id = %d', $pid ); $wpdb->query( $query2 ); } }

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