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.
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.
42 Responses
This is a great how-to post, this could be VERY usefull.
THINGS BLOGGERS COULD USE IT FOR:
- Affiliate offers that expire - eg, the STSE2 giveaway
- A Competition you are running
Anyone else have any ideas?
Thank you, Chris. This recipe can be used in a lot of different ways, thanks for your exemples!
Very cool! Do you know how to do it in the get_posts mini-loop?
Hi Mary-Ann, glad you liked this recipe! What do you mean by “Do you know how to do it in the get_posts mini-loop?”?
I use http://codex.wordpress.org/Template_Tags/get_posts quite a bit in static front pages and sidebars, so I was just wondering if it’s possible to integrate the expiration date code into it.
I don’t think this will work without using the loop. If you try, tell me if it worked for you!
If anyone else wants to do the same as me, I found http://wordpress.org/support/topic/205765 which I am going to try in the morning.
Hi I try this code at one of my testdomains, but it doesn’t work:(
maybe the time, is it possible that I need another form to write the value? At Europe we have today: 21.10.2008 and not 10/21/2008
regards
Monika
@Monika: I’m from Europe too
I’m sure it is possible to use this script with another date format than the default. The first thing to do is to check what date format are recorded on your database.
Can you tell me? Then I’ll try to help you
Great hack…
oh my God!
we have summer
and my server doesn’t know that we have summertime -
so all my tests must fail….
I’m sure the sky will fall on my head and something else behind the sky..
thanks for this code — now I can explain this in German
regards
Monika
For a long time, I used to delete old posts which has low value.
Glad to find this hack.
I’ll try it.
非常感谢,保持文章时效性正确是很重要的。
Hi Jean-Baptiste,
That’s a really cool trick! Nice…
@Stephen: Glad to see you on here
Nice one - thanks for the tip!
Need to know if the post are deleted or stored for future archiving - My problem is that I - need the post to go the archives and not to delete the post. Please let me know or if you can help me with a solution to this.
hi
i want display date on frontended from setting to admin
Hi, i was just wondering if anybody actually got this to work? I’ve tried to implement this by replacing the loop in my index.php with the code above. I had to take a ?> out of the code but thought that was just a typo. Any help?
I use this nice little plugin to expire posts on several of my WP sites: http://wordpress.org/extend/plugins/post-expirator/
How can I show a different template for post not expired (using a custom field with a date) only for a single category? For example show a red background only for post not expired in an event category.
How does this code actually work? It has two ?> php close tags in it. This code looks great but I haven’t found anyone who can actually use it correctly!
Trackbacks: