Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When I delete a user, WordPress can just delete the post or page of this user, not his custom post and his attachments.

An idea for a special hook?

add_action( 'delete_user', 'my_delete_user');

function my_delete_user($user_id) {
    $user = get_user_by('id', $user_id);
    $the_query = new WP_Query( $args );
        if ( have_posts() ) { 
            while ( have_posts() ) { 
                the_post(); 
                    wp_delete_post( $post->ID, false ); 

                    // HOW TO DELETE ATTACHMENTS ?
            }
        }
}
share|improve this question
1  
Hi, 24.743, welcome! Change your profile and your posts will always be signed Mic ;) - "Thanks" and signature are considered noise in the content and normally removed by your peers. Cheers! –  brasofilo Dec 12 '12 at 22:26
1  
Voilà! I update my profil. Thank! –  Mic Dec 14 '12 at 11:56

1 Answer 1

up vote 1 down vote accepted

The hook you choose is appropriate, and here is how to use it to delete all posts of all types (posts, pages, links, attachments, etc) of the deleted user:

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

If you only want to delete user attachments, change the post_type arguments from any to attachment and use wp_delete_attachment($attachment_id) instead of wp_delete_post().

share|improve this answer
    
Simple and perfect ! Thanks ! –  Mic Dec 14 '12 at 11:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.