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

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.

2 Responses

Jul 17 2011 04:14

Hi, I’ve used this code successfully, thanks so much!

First, I wanted to know what I could do to localize it, given that my page is in spanish and the date format is DD/MM/YYYY.

And second, I would like to display the expiration date with get_post_meta, and I would just like to display the date only as DD/MM/YY, without adding the time. Is there any way I can code that?

Thanks!!
Lili

Jul 17 2011 05:06

Another question, sorry!
I had already made this work in one site, but trying on another will not work…

Here’s what the code looked like on my index page:

And here is the modified code (changed the ‘expiration’ value to ‘vencecode’)

0 ) { include(TEMPLATEPATH . ‘/includes/entry.php’); } ?>

I’d appreciate if you could tell me where I went wrong, thanks!!

Lili

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required