Update post status from “publish” to “trash” for half of posts

I want to update half of all posts with status from published to trashed.

Example:

  • if I have 10000 posts, 5000 post to trash
  • If I have 6000 posts, 3000 posts to trash
  • If I have 7000 posts, 3500 posts to trash

Related posts

1 comment

  1. Assuming you want to delete by date, here is a basic concept. You’d need to figure out when you want this to run, otherwise, as I commented above, you’ll end up with only 1 post if it runs repeatedly.

    Edit: Including parameter for a bit of safety

    <?php 
    function doomsday_device($herd_thinning) {
        if(empty($herd_thinning)) {
            return;
        }
    
        $args = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'orderby' => 'date',
            'order' => 'ASC',
            'posts_per_page' => -1
        );
    
        $loop = new WP_Query($args);
    
        if($herd_thinning > $loop->post_count) {
            return;
        }
    
        $kill_num = $loop->post_count / 2;
        $i = 0;
    
        while($loop->have_posts()) : $loop->the_post();
            if($i >= $kill_num) {
                break;
            }
            wp_trash_post($post->ID);
            $i++
        endwhile;
    }
    ?>
    

    Must stress that this is a very dangerous function. If you attempt it, please back up your database. And you’ll need to pass an integer to it that represents a total post count size that is too large for your liking, which will then allow the thinning.

Comments are closed.