Opens in a new tab

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…

2 min read

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}

Related Posts

7 traits your engineering website must improve if you’re serious about generating new leads

In this post, I’m going to share everything I know about attracting good prospective engineering clients into your business. I’ve separated the post into 7 traits which contain the most important aspects of a professional engineering website. Improving any of these traits will help increase the productivity of your website – generating leads for your…

Read More

Search Engine Optimisation Basics

A lot of our work at Baltic Blog Design has been for very successful affiliates, and we think its worth sharing some tips and good SEO methodology that we’ve learnt or picked up along our merry way. Search Engine Optimisation, that was first left to hard working affiliates to figure out has now becoming a…

Read More