There are a number of reasons why you might want to set an expiration date on your WordPress posts. If you have a limited time giveaway discount code or a special event announcement on your Church website.
If you think of any other great uses, please, share!
Personally, I plan on using this for a Church website for upcoming events bundled in a custom post type. As soon as I have it built, I’ll be sure to share it with you all ;-).
In the meantime, here’s how you create an expiration date on your posts:
How-To Set an Expiration Date for Your Posts
Simply replace your WordPress Loop with this code:
[cc lang=”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 example…
the_title();
the_excerpt();
}
endwhile;
endif;
?>[/cc]
After you do this, you can use custom fields when writing a post to set an expiration date. Be sure to use the key expiration and use the the following date format: mm/dd/yyyy 00:00:00
I bet you’re wondering what happens to the post and what it might do to broken links if someone links to you or you link from another post, right?
No worries.
This doesn’t remove or unpublish the post, but simply excludes the article from being displayed in the loop.
Cool, right!?!
[via WP Beginner]
Dustin W. Stout says
This is a great idea Eric! I’m not quite sure what you mean by “replace your wordpress loop” though…?
Eric Dye says
Replace might not be the best words. Perhaps, edit? Here’s some info on the Loop: http://codex.wordpress.org/The_Loop
You’re running Standard, so depending on what you want to do, create a custom loop and call it out with a custom page.
Tony Gray says
Very Cool, Im doing the same sorta thing on several sites. My rotating banner images on http://desotohillsbaptist.com have an expiration date so the volunteers that maintain the site dont have to remember when to take down a banner. I added a jQuery popup date picker to the custom post type to make it easier for them to pick a date. The users really like it.
Eric Dye says
I love the date picker add – FTW!
Jeff Lovell says
Good tip. I’ve been using the Post Expirator (http://wordpress.org/extend/plugins/post-expirator/) plugin to do the same thing for quite a while. For those who aren’t into editing their files, it’s worked well.
Eric Dye says
🙂
Broeiend says
Very nifty feature. However this isn’t working for me, I live in the Netherlands and wordpress doesn’t support the local time in posts.
So if i set the time expiration date, it doesn’t match with my local time. 🙁