您选择的挂钩是合适的,下面是如何使用它删除已删除用户的所有类型(帖子、页面、链接、附件等)的所有帖子:
add_action(\'delete_user\', \'my_delete_user\');
function my_delete_user($user_id) {
$args = array (
\'numberposts\' => -1,
\'post_type\' => \'any\',
\'author\' => $user_id
);
// get all posts by this user: posts, pages, attachments, etc..
$user_posts = get_posts($args);
if (empty($user_posts)) return;
// delete all the user posts
foreach ($user_posts as $user_post) {
wp_delete_post($user_post->ID, true);
}
}
如果只想删除用户附件,请更改
post_type
参数来自
any
到
attachment
和使用
wp_delete_attachment($attachment_id)
而不是
wp_delete_post()
.