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.
60 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!
Hi – got diverted here via smashing magazine – I’m in the UK and my server is running the format: yyyy-mm-dd hh:mm:ss
What would I need to alter to get the above code to work – it would be of considerable help on my site as I am manually expiring contributor posts at present. Also, – I have multiple loops on my pages – I assume I can just add this one too?
Thanks so much.
do you use this in the index.php or the single.php
hei great code!!
just one questions…
is possible to change date’s format like ( dd/mm/yyyy ) and use it without time spacification…
thanks.. and sorry for my bad english…
Could you tell me how to check the date format of server?
Thanks
It would be nice if you could set it up to expire something like media.. For example if you wrote podcasts or something along those lines.
Any ideas?
Thanks. it’s usefull tips. I’ll try later ASAP
Thanks for sharing, had tried it out,no problem so far. Awesome shares.
But i got a little problem, which is the pagination. Example in total i have 100 posts, and 60 of the posts already expired. But the next page button still allow me to click next page even though there is no more post, do anyone have any idea how to fix this?
Sorry, but I don’t understand where exactly I have to put this code inside the loop! Any help?
Hi PP,
u need to place the code inside the index.php
This script is good as a quick solution but we definitely need something that will change the status of the post when the expiration time kicks in.
The posts are still published. They definitely show in the feed and in other listings and if you don’t place this code everywhere in your theme, you will always have the expired posts listed. (as well as the buttons for next pages)
Best scenario: a cron job with a bit of code to change the status of the expired posts.
Is it possible to set an expiration date only for a given custom field? For example: a custom field contains a description which will disappear after the expiration date. The post remains, just the value of that custom fields disappear… Hm?
I’m using the Set post expiration date/time but I’m having a very simple problem but can seem to remedy it.
In setting up an else statement such as: “There is nothing scheduled.” The else statement is being recognized and echoed due to the expired posts within the while loop. So if there are expired events it prints the else statement for each expired event.
I’d like to accomplish an else statement if there are no events greater than today then “There is nothing scheduled.”
Trackbacks: