Pages

How set an Expiration Date for Your Posts







This hack comes becomes very useful when you are running a contest because you might be posting information such as clues or hints that you don’t want to stay up for ever. Instead of manually removing the article, you can just make it expire automatically. It also works if you have a product that you are offering a discount on. You posted it on your blog, but you don’t want that discount to stay on your blog after its over. So you can remove it automatically with this code.
All you need to do is replace your WordPress Loop with this code:

01<?php
02if (have_posts()) :
03while (have_posts()) : the_post(); ?>
04$expirationtime = get_post_custom_values('expiration');
05if (is_array($expirationtime)) {
06$expirestring = implode($expirationtime);
07}
08
09$secondsbetween = strtotime($expirestring)-time();
10if ( $secondsbetween > 0 ) {
11// For example…
12the_title();
13the_excerpt();
14}
15endwhile;
16endif;
17?>
Once you have done that, you can use custom fields when writing a post to set an expiration date. Make sure you select the key “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.

No comments:

Post a Comment