1

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
1
  • 1
    I assume you want to run this under special circumstances. Otherwise it would keep dividing itself until you have only 1 post left...
    – GhostToast
    May 31, 2013 at 16:01

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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