如果我正确理解了这个问题,您希望每当用户添加新帖子时,他/她以前的所有帖子都应该删除。我想知道你为什么要那样做!!:)但无论如何,您可以使用此代码。
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 );
endwhile;
endif;
wp_reset_query();
}
}
}
但是,您可能需要根据您的要求更改用户角色