Created
March 4, 2014 17:42
-
-
Save nicholasohrn/9351687 to your computer and use it in GitHub Desktop.
Reassign posts from user being deleted
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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