delete all post and attachments of a user delete codegrapper code example

Example: delete all post and attachments of a user delete codegrapper

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);
    }
}

Tags:

Php Example