0

For a long time, I have searched for a plugin that will delete posts that are over a certain 'age' (eg. 30 days old). What is the best way to do this?

2
  • Do you want to have this as something that happens inside Wordpress, or would you be equally happy with a cronjob? Commented Mar 15, 2011 at 20:29
  • it's good...in any case :-) or rather, i think it should be already implemented into wordpress and i don't understand why the trash subject is not really considered by developers. Thanks
    – dongio
    Commented Mar 15, 2011 at 21:29

3 Answers 3

5

Here's some SQL that will find every post that has been around for 30 days or more:

SELECT * FROM `wp_posts`
WHERE `post_type` = 'post'
AND DATEDIFF(NOW(), `post_date`) > 30

To delete all the posts, you can replace SELECT * FROM with DELETE FROM — but make sure you take a backup before you do this!

You can then just cron that however you like, be that a shell script, a PHP script, or whatever you're most comfortable with.

8
  • thank you very much, how to integrate this into a plugin calling wp cron once a day?
    – dongio
    Commented Mar 15, 2011 at 22:00
  • It can exist entirely outside of Wordpress if you want to keep it simple, just write some PHP that connects to your database and runs the query. There are plenty of good MySQL/PHP tutorials out there. Commented Mar 15, 2011 at 22:03
  • If it's solved, don't forget to mark the best answer - it's the green tick on the left hand side. Commented Mar 15, 2011 at 22:12
  • Hi Sam, if this answer solved your problem you should hit the "checkmark" next to it :) Commented Mar 15, 2011 at 22:13
  • @themerlinproject Don't worry, I'm well-versed in SO - just mentioned the same thing to the OP :) Commented Mar 15, 2011 at 22:15
4
delete a,b,c,d
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
LEFT JOIN wp_terms e ON ( e.term_id = d.term_id )
WHERE e.term_id =<category id>
AND a.post_type = 'post'
AND DATEDIFF(NOW(), a.post_date) > 30

Reference

0

Delete 30 days old post command is :

delete FROM `wp_posts`
WHERE `post_type` = 'post'
AND DATEDIFF(NOW(), `post_date`) > 30
1
  • Could you please specify how your answer differs from @Sam answer? Commented May 27, 2013 at 17:32

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.