Skip to content

Instantly share code, notes, and snippets.

@nicholasohrn
Created March 4, 2014 17:42
Show Gist options
  • Save nicholasohrn/9351687 to your computer and use it in GitHub Desktop.
Save nicholasohrn/9351687 to your computer and use it in GitHub Desktop.
Reassign posts from user being deleted
<?php
function reassign_post_on_delete_user($user_id) {
global $wpdb;
// Replace the following with whatever ID you want to reassign to
$replacement_id = 1;
$post_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_author = %d", $user_id));
$wpdb->update($wpdb->posts, array('post_author' => $replacement_id), array('post_author' => $user_id));
foreach($post_ids as $post_id) {
clean_post_cache($post_id);
}
}
add_action('delete_user', 'reassign_post_on_delete_user');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment