UPDATED May 6, 2006: This hasn’t been tested in WordPress 2.0. According to this WordPress forum thread, this plugin works in 2.0, so give it a try!
I’ve been helping a friend convert her website to a WordPress CMS and ran into a problem.
FRIEND: With my old software, I could type in a date on any specific post and on that date it would be taken off the website. It was really easy. Can I do that with this… WordPress?
ME: Sure! WordPress can do anything! If there isn’t a feature built-in, somebody’s always designed a plugin for it.
FRIEND: Great. Can we do it now?
ME: Er… let me get back to you on this.
In the frantic searching that ensued, I found Ashwin Bihari’s plugin: Auto Delete Posts.
This is a great little plugin. It works by taking the current date and determining which posts are older than 14 days (14 days is the default, but you can set another number of days) and if any posts are found, they are deleted or moved to another category.
But this wasn’t what my friend wanted. She wanted some posts to automatically expire after 7 days, some after 30 days, and so on.
I did some more research and found this WordPress forum thread: “Post expires after set date or number of days,” which recommends following the WordPress Wiki instructions “Expire Posts.”
Expiring posts turns out to be something that can be done with Custom Fields values. (It says you can do this in the Codex, too, but doesn’t explain how.)
The directions are set up for WordPress version 1.2, but the Wiki says it works for 1.5, too. The index.php page has changed from 1.2 to 1.5, but I figured it out (I thought) and inserted the necessary lines of code. My index.php now looked like this:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php require('post.php'); ?>
<?php list ($post_expired) = get_post_custom_values('postexpire');
/* only pay attention to the first one if there's a duplicate */ ?>
<?php if ( !$post_expired || current_time('mysql') <$post_expired) :
/* if there's no expiration date or it's not been reached yet, go
ahead with displaying the post */ ?>
<?php endif; /* end not-expired test */ ?>
<?php endwhile; ?>
Then, in the Custom Field area of the Dashboard, I created a “postexpire” Key and a Value of “2005-10-03 01:00:00.”
I didn’t get any error messages, but the post didn’t expire as scheduled, either. I wrote in the WordPress forum for help, but nobody had an answer for me. Almost 2 days later I had to tell my friend I was still working on it.
Anxious not to lose face, I looked back to Ashwin’s plugin. I wrote him and asked if perhaps there was some way I could set his plugin to allow for different expiration values. He kindly wrote back to say no, and then offered to help me with my Custom Field values.
At that moment, this is what I thought of Mr. Ashwin Bihari:
He suggested I rearrange the lines to look like so:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php require('post.php'); ?>
<?php list ($post_expired) = get_post_custom_values('postexpire'); /* only pay attention to the first one if there's a duplicate */ ?>
<?php if ( !$post_expired || current_time('mysql') < $post_expired) : /* if there's no expiration date or it's not been reached yet, go ahead with displaying the post */ ?>
<?php endif; /* end not-expired test */ ?>
<?php endwhile; ?>
It works perfectly. The posts are not deleted. They remain in the database where you can manage them on the “Dashboard –> Manage –> Posts” screen.
You can “un-expire” them, or delete them, or whatever it is you want to do with an expired post. But as long as the date you posted in the Custom Fields Value box matches or is after today’s date, the post will not appear as published.
Ashwin now joins the growing ranks of those selfless WordPress gurus who have saved Bonnie’s bohunney. Thank you, Ashwin!