Wordpress Plugin: Delete, state or move expired posts

I was search for a plugin that can let expire posts in Wordpress and found this wonderfull plugin: Astros Wordpress Plugin. It's really a good idea and it's a good implementation as well.

Unfortunatly it wasn't exactly what I needed, because it deleted expired posts irreversibly. But I wanted to be able to set a post to "draft" or to move it to a special category if it had expired. So I extended the plugin. Have fun.

Download Plugin.
Please keep in mind: This plugin has been tested, but only on some systems. It is a "beta-version" which means: Be careful using it on production data. Deleted posts are deleted forever, so backup and test. Suggestions and error-messages are welcome, please mail to: info@thomatechnik.de. Please don't be angry if I have no time to answer you.

How to use?

Have a look at the file atropos.php in the zip-folder. At line 23 you will find:
function atropos_delete_expired_posts () {
  global $wpdb;
  $result = $wpdb->get_results("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_atropos_expiration_date' AND meta_value < '" . date("Y/m/d") . "'");
  foreach ($result as $a) {
  
	// Delete post if post is expired
	/* wp_delete_post ($a->post_id);
	*/
	
	// Set new post_status if post is expired
	/*$my_post = wp_get_single_post($a->post_id, ARRAY_A);
	$my_post['post_status'] = 'pending'; // draft
	wp_insert_post($my_post);
	*/
	
	// Move expired posts to category "expired"
	/*$my_post = wp_get_single_post($a->post_id, ARRAY_A);
	$my_cat = get_category_by_slug("expired");
	$my_post['post_category'] = array((int)$my_cat->term_id);
	wp_insert_post($my_post);
	*/
	}
}

See the 3 blocks which are commented out?

  • Uncomment the first one to delete the expired posts.
  • Uncomment the second block if you want to set expired posts to a special post_state like "pending" or "draft". You can choose the state in the line $my_post['post_status'] = 'pending'
  • Uncomment the third block if you want to move the post to a category. You can choose to which category they are moved in the line $my_cat = get_category_by_slug("expired"); (just change expired to the category you want).

Tipps

The expire-script now does not only run at midnight, but when you activate the plugin as well. So if you want to test the plugin, just deactivate and activate the plugin again. If you have still problems testing it, uncomment the last line - it will trigger the expire-script EVERYTIME a page of your site is browsed.

Futher improvements

I think the server-load is not too much to let the script run hourly. For this the form has to be extended to set the expiration-hour as well, and the database has to be extended to store the hour.

What do you want for that?

Well, I spend some time on it, so if you use it on your wordpress-site, you might consider to set a link to the original author and to this site to credit the work.