Recently a client requested that they wanted to put an expiry date on a blog post, so the post would be removed from the index and category pages after the offer expired. For SEO purposes, the post would still exist at it’s original URL and be able to be accessed through an archives page.
It’s a really good idea, as many affiliates offering discount codes and offers would find this added function very useful.
A few plugins exists that will “expire” a post, but unfortunately they either delete the post, or move it to a draught version that can only be seen in Wordpres admin. This is bad for SEO. Old links expired or not, can still bring in traffic to a website and if a customer missed the offer the first time, they still know they should bookmark the site for the next time around.
SNIPPLR have written a modified version of the Wordpress Loop which will remove posts after a date that can be specified in the Custom Fields section of the WP text editor. The code compares an expiry date given with the date from the host server and if the date has passed, the wordpress loop will omit that post from being shown.
The code doesn’t show the posts in the archives however and only expires posts by date when our client needed a to expire a post at a specific time. We modified SNIPPLR’s code so it would do both and also added the ability to adjust the server time, which is handy if your website is hosted in a different timezone to the one you need to use.
All you need to do when writing the post, is enter a custom field with the key “expiration” and set the value in the format: “2010/01/01 18:20:00″ – this would expire the post at 6.20pm on the 1st January 2010.
<?php //to check against expiration date;
$timestamp = strtotime("now + 8 hours");
$currentdate = date('YmdHis', $timestamp);
$expirationdate = get_post_custom_values('expiration');
if (is_null($expirationdate)) {
$expirestring = '30005050235959'; //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP;
} else {
if (is_array($expirationdate)) {
$expirestringarray = implode($expirationdate);
}
$markup = array("/",":"," ");
$expirestring = str_replace($markup,"",$expirestringarray);
} //else
if (( $expirestring > $currentdate ) || (is_archive())) { ?>
***THEN PUT THE FOLLOWING LINE AT THE VERY END OF THE LOOP***
<?php } //end if for expiration; ?>









Hi, I’m Dan Cregin. I’m a freelance web developer, designer and SEO consultant, specialising in creating custom themes, plugins and modifications for WordPress. 


July 26th, 2009 at 2:57 pm
Hi – this looks to be exactly what I need. However, I am having trouble getting it to work. I am using it on my archives.php file (which contains a few loops) immediately after “” which precedes the content of the post – and then adding the end code you suggest here which then details the next loop content giving previous and next links.I know my server is using the format yyyy/mm/dd hh:mm:ss and I have created the custom field “expiration”. Unfortunately – no luck getting the post to disappear at the time and date I specify. Have I got the code in the wrong place perhaps?
I would realy appreciate it if you can help me out.
Many thanks!