How set an Expiration Date for Your Posts

Posted on February 18th, 2011

Instead of manually removing the article, you can just make it expire automatically. It also works if there is a product which is offered a discount. Let us assume that this is posted on the blog. And if this discount has to be removed from the blog after it is over, this code is used. The code removes it automatically.

Just  replace the WordPress Loop with this code:

$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// For example…
the_title();
the_excerpt();
}
endwhile;
endif;
?>

Once this is done, custom fields can be used when writing a post to set an expiration date. Make sure the key selected is “expiration” and use the the following date format: mm/dd/yyyy 00:00:00. Now this hack does not remove or unpublish the article instead it just excludes the article from being displayed in the loop.