How to: Set post expiration date/time on your WordPress blog

October 20, 2008 at 9:09 am

Do you regret that WordPress haven’t a feature to publish a post during only 2 days, or one week? Here is a very nice code that you can use in your WordPress theme, to enable the possibility of creating post expiration based on date and time.

Edit your theme and replace your current WordPress loop by this “hacked” loop:

<?php
if (have_posts()) :
     while (have_posts()) : the_post(); ?>
         $expirationtime = get_post_custom_values('expiration');
         if (is_array($expirationtime)) {
             $expirestring = implode($expirationtime);
         }

         $secondsbetween = strtotime($expirestring)-time();
         if ( $secondsbetween > 0 ) {
             // For exemple...
             the_title();
             the_excerpt();
         }
     endwhile;
endif; 
?>

To create a post with date/time expiration, just create a custom field. Give it expiration as a key and your date/time (format: mm/dd/yyyy 00:00:00) as a value.
The post will not show after that time stamp.