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}

With a special focus on e-commerce, web development, and UX/UI design, we are the go-to choice for businesses not only in Newcastle and the North East but also throughout the UK.

Located in Gateshead, Baltic Design is at the forefront of the digital landscape, offering expert troubleshooting, speed optimisation, and robust hosting solutions.

Best Website designers in Gateshead
4.9 Stars - Based on 7 User Reviews

Get in touch!

55 Oakfield Road Lobley Hill, Gateshead NE11 0AA
Monday, Tuesday, Wednesday, Thursday, Friday09:00 – 17:00
07903246237
Copyright © 2024 Baltic Design. All Rights Reserved..