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.

I am an admin of a multi-author blog. I am implementing a monitoring system which will require me to downgrade a user from an 'author' (this role allows them to make a custom post) back to the 'reader' role if I think it is a spam account, or if they are breaking website rules.

After changing their role using the admin screen, how can I make all the posts they made delete automatically, without having to scroll and find them myself?

Many thanks

EDIT using advice from answers below:

add_action( 'set_user_role', 'wpse98904_remove_demoted_user_posts', 10, 2 );
function wpse98904_remove_demoted_user_posts( $demoted_author_id, $role ) {
 // In here you'd search for all the posts by the user

 $args = array(
            'numberposts' => -1,
            'author' => $demoted_author_id,
            'post_type' => 'custom_post',
            // So if your custom post type is named 'xyz_book', then use:
            // 'post_type' => 'xyz_book',
 );
 $demoted_author_posts = get_posts( $args );
 foreach( $demoted_author_posts as $p ) {
     // delete the post (actually Trash it)
     if($role == 'subscriber') {
     wp_trash_post( $p->ID);
     // To force the deletion (ie, bypass the Trash):
     // wp_delete_post( $p->ID, true );
     }
 }
}

I used wp_trash_post to trash the events because adding 'false' to wp_delete_post did not work for me.

share|improve this question
    
If you delete the user, you will have the option of removing their posts. But if you just want to demote them, then I guess that won't cut it. –  vancoder May 9 '13 at 16:28

2 Answers 2

up vote 2 down vote accepted

You can add actions to the set_user_role hook:

add_action( 'set_user_role', 'wpse98904_remove_demoted_user_posts', 10, 2 );
function wpse98904_remove_demoted_user_posts( $demoted_author_id, $role ) {
     if( 'subscriber' == $role ) {
         // In here you'd search for all the posts by the user
         $args = array(
                'numberposts' => -1,
                'author' => $demoted_author_id,
                'post_type' => '{your custom post type name}',
                // So if your custom post type is named 'xyz_book', then use:
                // 'post_type' => 'xyz_book',
         );
         $demoted_author_posts = get_posts( $args );
         foreach( $demoted_author_posts as $p ) {
             // delete the post (actually Trash it)
             wp_delete_post( $p->ID );
             // To force the deletion (ie, bypass the Trash):
             // wp_delete_post( $p->ID, true );
         }
     }
}

Reference

set_user_role hook -- Codex

set_user_role hook in WP Trac

wp_delete_post() -- Codex

share|improve this answer
    
Ah, just barely beat me to it. :) –  Andrew Bartel May 9 '13 at 16:49
    
@AndrewBartel Sometimes I win the race, sometimes I lose it... –  Pat J May 9 '13 at 18:22
    
@PatJ Thanks it deleted the posts as aksed. But this function applied to all users, including my fellow admins. How can i alter the function further so that this function only applies to 'Author' users. –  Adzay May 9 '13 at 22:24
    
Add in the if statement from my answer @Adzay –  Andrew Bartel May 9 '13 at 23:30
    
@Adazy I've updated my code to include Andrew Bartel's if statement. –  Pat J May 10 '13 at 12:56

Users roles are changed by the WP_User object firing the set_role() function. At the end of that function on line 815 of wp-includes/capabilities.php there is an action to hook to: do_action( 'set_user_role', $this->ID, $role );

So, in your functions.php or in a plugin, you can grab that data as the hook fires after the user capability update, and delete all of a user's posts with wp_delete_post.

add_action('set_user_role','myfunc',10,2);
function myfunc($user_id,$role) {
    if($role == 'subscriber') { // or whatever you want
        $posts = get_posts('numberposts=-1&author='.$user_id);
        foreach($posts as $post) {
            wp_delete_post($post->ID,true);
        }
    }
}

Now, be careful, because as is, this snippet will permenently delete the post. If you want to just move it to trash, change the second parameter or wp_delete_post to false.

share|improve this answer
    
Looking at the source of the set_user_role hook, it looks like $role is the new role of the user. If I'm reading your code right, it will delete all posts for a user who has just been changed to the Author role -- which I don't think is quite what is asked for. –  Pat J May 9 '13 at 18:50
    
Right, thus the comment. But you're right that if copy pasted it would nuke someone's posts who was just promoted to author. I'll edit for readability. –  Andrew Bartel May 9 '13 at 20:13
    
@PatJ Hi thanks for that. Unfortunately it doesn't work for me. I should have added that it was a custom post type that I was trying to delete, is that why it didn't work? I tried to add &post_type=Custom to get_posts() but still ... no luck. –  Adzay May 9 '13 at 21:17
    
I'll edit my code to take the CPT thing into account. –  Pat J May 9 '13 at 21:18

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.