How to set expiration date for your blog post in WordPress?

This wordpress tip comes to your rescue when you want a certain blog post to show only for a limited period of time like a contest or may be a discount offering post. Instead of manually removing the blog post, you can just make it expire automatically.All you need to do is replace your WordPress Loop with this code:

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

One comment

Leave a Reply

%d bloggers like this: