• I have been working on automating a bunch of stuff on several of my sites and I’ve run into some interesting questions about Trash. I’ve read the basic info on WP.com but it doesn’t cover all the ins and outs, so I thought I’d ask my questions here.

    1. Trash collection is every 30 days: I’ve adjusted this in my wp-config to 1 day. When does the trash get collected and how is it triggered? Is it at midnight? Is it part of wp-cron? Is it x days to-the-minute or removed “after x days” (or x+1 days to allow a whole day in trash)?

    2. What happens if I set the number of days to zero, then use SQL to adjust post_status to ‘trash’? Will those posts ever get deleted?

    3. When items in trash are removed, do the attachments go with them (i.e. deleted permanently) or does WP do the same as usual and leave them alone?

    I have a horrible, sinking feeling that I’m going to have to build my own plugin to do what (to me) seems normal. Any info appreciated!

Viewing 1 replies (of 1 total)
  • Thread Starter SpikeTheLobster

    (@spikethelobster)

    In case it will help other people, some discoveries:

    2. This is a bad idea. Setting post_status to ‘trash’ via SQL apparently circumvents some built-in functionality, the practical result of which is that the posts get deleted but the attachments and everything else stay put. Very messy.

    I was doing an UPDATE query with $wpdb->get_results(). Instead, it’s better to do a SELECT query with $wpdb->get_results() and pass the results to a foreach loop, like this (to trash posts after 30 days):

    $dayquery = "SELECT * FROM $wpdb->posts
                 WHERE $wpdb->posts.post_status = 'publish'
                 AND $wpdb->posts.post_type = 'post'
                 AND $wpdb->posts.post_date < DATE_SUB(NOW(), INTERVAL 30 DAY)";
    
    $result = $wpdb->get_results($dayquery);
    foreach ($result as $post) {
    	setup_postdata($post);
    	$postid = $post->ID;
    	wp_delete_post($postid);
    }

    Hopefully this will fully trash items (including attachments).

Viewing 1 replies (of 1 total)
  • The topic ‘Default Trash behaviour question’ is closed to new replies.