dongio
dongio

Reputation: 3

Wordpress: automatically delete posts that are x days old?

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?

Upvotes: 0

Views: 12889

Answers (3)

Amit Kumar Gupta
Amit Kumar Gupta

Reputation: 7413

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

Upvotes: 4

user2425846
user2425846

Reputation: 1

Delete 30 days old post command is :

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

Upvotes: 0

Sam Starling
Sam Starling

Reputation: 5378

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.

Upvotes: 5

Related Questions