Codex tools: Log in
Contents |
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.
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;
}
?>