WordPress Code: Expire Posts to Archive using Custom Fields

Recently a client requested that they wanted to put an expiry date on a blog post, so the post would be removed from the index and category pages after the offer expired. For SEO purposes, the post would still exist at it’s original URL and be able to be accessed through an archives page.

It’s a really good idea, as many affiliates offering discount codes and offers would find this added function very useful.

A few plugins exists that will “expire” a post, but unfortunately they either delete the post, or move it to a draft version that can only be seen in WordPress admin. This is bad for SEO. Old links expired or not, can still bring in traffic to a website and if a customer missed the offer the first time, they still know they should bookmark the site for the next time around.

SNIPPLR have written a modified version of the WordPress Loop which will remove posts after a date that can be specified in the Custom Fields section of the WP text editor. The code compares an expiry date given with the date from the host server and if the date has passed, the wordpress loop will omit that post from being shown.

The code doesn’t show the posts in the archives however and only expires posts by date when our client needed a to expire a post at a specific time. We modified SNIPPLR’s code so it would do both and also added the ability to adjust the server time, which is handy if your website is hosted in a different timezone to the one you need to use.

All you need to do when writing the post, is enter a custom field with the key “expiration” and set the value in the format: “2010/01/01 18:20:00” – this would expire the post at 6.20pm on the 1st January 2010.

{code type=PHP}***PUT THIS INSIDE THE LOOP, FIRST THING***

$currentdate ) || (is_archive())) { ?>

***THEN PUT THE FOLLOWING LINE AT THE VERY END OF THE LOOP***

{/code}

5 Comments. Leave new

Hi – this looks to be exactly what I need. However, I am having trouble getting it to work. I am using it on my archives.php file (which contains a few loops) immediately after “” which precedes the content of the post – and then adding the end code you suggest here which then details the next loop content giving previous and next links.

I know my server is using the format yyyy/mm/dd hh:mm:ss and I have created the custom field “expiration”. Unfortunately – no luck getting the post to disappear at the time and date I specify. Have I got the code in the wrong place perhaps?

I would realy appreciate it if you can help me out.

Many thanks!

Reply

Hello..thanks so much for this code! Just curious..i was able to get this working on the home page loop, but what about for the categories? Is it the same code?

Reply

Hi Chris, it will be the same code exactly. The code is just looking for the custom field info and then executing whatever loop is contained.

Reply

hiya

any chance of giving an example where we fit the above within a loop?

i would really appreciate it

thanks
T

Reply

Hi SEOrocks – this is within the loop! Just have a go and you’ll get it eventually 😀

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *