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 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
share|improve this question
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 '13 at 16:01
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
add comment

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.