How to: Set post expiration date/time on your WordPress blog

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

Oct 20 2008 12:20

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?

Oct 20 2008 12:28

Thank you, Chris. This recipe can be used in a lot of different ways, thanks for your exemples!

Oct 20 2008 17:31

Very cool! Do you know how to do it in the get_posts mini-loop?

Oct 20 2008 17:44

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?”?

Oct 20 2008 19:08

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.

Oct 20 2008 19:57

I don’t think this will work without using the loop. If you try, tell me if it worked for you!

Oct 20 2008 21:30

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.

Oct 21 2008 19:44

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

Oct 21 2008 20:03

@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 :)

Oct 22 2008 08:55

Great hack…

Oct 22 2008 21:35

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

Oct 24 2008 04:59

For a long time, I used to delete old posts which has low value.
Glad to find this hack.

I’ll try it.

非常感谢,保持文章时效性正确是很重要的。

Oct 30 2008 05:37

Hi Jean-Baptiste,

That’s a really cool trick! Nice…

Oct 30 2008 08:11

@Stephen: Glad to see you on here :)

Nov 09 2008 19:30

Nice one – thanks for the tip!

Feb 12 2009 10:48

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.

Mar 23 2009 22:09

hi
i want display date on frontended from setting to admin

Apr 01 2009 17:46

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?

May 13 2009 14:28

I use this nice little plugin to expire posts on several of my WP sites: http://wordpress.org/extend/plugins/post-expirator/

May 13 2009 17:06

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.

May 14 2009 05:10

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!

Jul 26 2009 13:50

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.

Aug 14 2009 19:13

do you use this in the index.php or the single.php

Aug 21 2009 18:00

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…

Jan 22 2010 01:11

Could you tell me how to check the date format of server?
Thanks

Feb 10 2010 23:58

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?

Apr 16 2010 21:19

Thanks. it’s usefull tips. I’ll try later ASAP

May 02 2010 17:17

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?

May 05 2010 22:10

Sorry, but I don’t understand where exactly I have to put this code inside the loop! Any help?

May 08 2010 12:26

Hi PP,

u need to place the code inside the index.php

May 20 2010 21:46

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.

Jun 22 2010 15:12

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?

Jul 13 2010 18:47

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:

Leave a Comment

* Name, Email, Comment are Required