Codex

Plugin API/Action Reference/delete post

Contents

Description

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

However by this time the post comments, attachments 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 a synchronous synchronisation is to be made aware when a post is deleted so that you can replicate the changes yourself.

 <?php
add_action
('admin_init''codex_init');
function 
codex_init() {
  if (
current_user_can('delete_posts')) add_action('delete_post''codex_sync'10);
}

function 
codex_sync($pid) {
  global 
$wpdb;
  if (
$wpdb->get_var($wpdb->prepare('SELECT post_id FROM codex_postmeta WHERE post_id = %d'$pid))) {
    return 
$wpdb->query($wpdb->prepare('DELETE FROM codex_postmeta WHERE post_id = %d'$pid));
  }
  return 
true;
}
?>

Related

wp_delete_post() before_delete_post()