Codex

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

Plugin API/Action Reference/delete post

This page redirects to an external site: https://developer.wordpress.org/reference/hooks/delete_post/

Description

delete_post is fired before and after a post (or page) is deleted from the database.

However by this time the post comments and metadata have been already deleted. Use the before_delete_post hook to catch post deletion before that.

Definitions

/wp-includes/post.php - pre-deletion
(#2041) (integer) The $postid parameter is the same value that was passed to wp_delete_post().
Hook: do_action( 'delete_post', $postid );
/wp-includes/post.php - post-deletion
(#2043) (integer) The $postid parameter is the same value that was passed to wp_delete_post().
Hook: do_action( 'deleted_post', $postid );

Example

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 synchronisation is to be made aware when a post is deleted so that you can replicate the changes yourself.

<code style="color: #000000"><span style="color: #0000BB"><?php
add_action</span><span style="color: #007700">( </span><span style="color: #DD0000">'admin_init'</span><span style="color: #007700">, </span><span style="color: #DD0000">'codex_init' </span><span style="color: #007700">);
function </span><span style="color: #0000BB">codex_init</span><span style="color: #007700">() {
    </span><span style="color: #0000BB">add_action</span><span style="color: #007700">( </span><span style="color: #DD0000">'delete_post'</span><span style="color: #007700">, </span><span style="color: #DD0000">'codex_sync'</span><span style="color: #007700">, </span><span style="color: #0000BB">10 </span><span style="color: #007700">);
}

function </span><span style="color: #0000BB">codex_sync</span><span style="color: #007700">( </span><span style="color: #0000BB">$pid </span><span style="color: #007700">) {
    global </span><span style="color: #0000BB">$wpdb</span><span style="color: #007700">;
    if ( </span><span style="color: #0000BB">$wpdb</span><span style="color: #007700">-></span><span style="color: #0000BB">get_var</span><span style="color: #007700">( </span><span style="color: #0000BB">$wpdb</span><span style="color: #007700">-></span><span style="color: #0000BB">prepare</span><span style="color: #007700">( </span><span style="color: #DD0000">'SELECT post_id FROM codex_postmeta WHERE post_id = %d'</span><span style="color: #007700">, </span><span style="color: #0000BB">$pid </span><span style="color: #007700">) ) ) {
        </span><span style="color: #0000BB">$wpdb</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">( </span><span style="color: #0000BB">$wpdb</span><span style="color: #007700">-></span><span style="color: #0000BB">prepare</span><span style="color: #007700">( </span><span style="color: #DD0000">'DELETE FROM codex_postmeta WHERE post_id = %d'</span><span style="color: #007700">, </span><span style="color: #0000BB">$pid </span><span style="color: #007700">) );
    }
}
</span><span style="color: #0000BB">?></span></code>

Related

wp_delete_post() before_delete_post()