WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hope you can point me in the right direction, with a plugin or function. With "Contact Form 7" logged in users post their info with "Form to Post" plugin. Everything is front end. It is a big form that uses custom fields to display posts. I need a way to automatically delete posts, so every time a new post by user is created the last one is deleted in that category. I found a way I can do it by date, by that will not work. Thanks.

share|improve this question
up vote 0 down vote accepted

If i understand the question correctly, you want that whenever a user adds a new post, all his/her previous posts should be deleted. I wonder why would you do that!! :) But anyway, you can use this code.

add_action('save_post', 'delete_prev_posts_by_user', 10, 2 );
function delete_prev_posts_by_user( $post_id, $post ){
    if( 'publish' == $post->post_status ){
        $the_special_category_id = 1;//the id of the concerned category 
        $post_categories = wp_get_post_categories( $post_id );

        $user = new WP_User( $post->post_author );
        if( in_array( $the_special_category_id, $post_categories ) && 'contributor' == $user->roles[0] ){
            $userposts = new WP_Query( array( 'author'=>$post->post_author, 'post__not_in'=>array( $post_id ), 'cat'=>$the_special_category_id ) );
            if( $userposts->have_posts() ): while( $userposts->have_posts() ): $userposts->the_post();
                //set the second parameter to false, to trash it rather than deleting permanently
                wp_delete_post( get_the_ID(), true );

You might want to change the user role though, according to ur requirement

share|improve this answer
I have a form (fitness profile calculator) for their private online journal. It will post in category by author. My fear is they will keep resubmitting it to update it until they like what they see. Will this delete the post or trash it? Is it possible to to set this for just one category? – user8514 May 15 '13 at 22:07
edited my answer, now it'll delete previous posts from only the given category. you need to set $the_special_category_id value with the id of category u talking about – webdwall May 16 '13 at 6:37
WOW! That is great! Thank you so much. – user8514 May 16 '13 at 14:57

Your Answer


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.