DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

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.

6 Comments

  1. 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

  2. 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

  3. Hello,

    thanks for your post. Is there any way we can show a custom message on expired post.

    1. We should not delete or disappear those.
    2. It shows a custom message on top of post as “Post expired”

    pls advice.

  4. Great stuff, just to make the code better, how can ?I make it show if no custom field key is present for expiration?

  5. Thank you very much! I use it for a sidebar that show future events and not the past ones.

    It’s possible to still show today posts? If the expirationtime it’s today the post doesn’t show with your process.

    Thank you, Diego

  6. hi great stuff!

    i just have a question.

    does “not show” meaning it will be deleted or it will be set as unpublish?

    thanks!

    hope to learn more tweaks from you guys!

Leave a Comment

Your email address will not be published. Required fields are marked *