Return to Snippet

Revision: 42418
at March 4, 2011 20:25 by ramborc


Initial Code
***PUT THIS INSIDE THE LOOP, FIRST THING***

<?php //to check against expiration date;

$todays_date = date("d.m.Y");

$today = strtotime($todays_date);

$expirationdate = get_post_custom_values('expiration');

if (is_null($expirationdate)) {

$expiration_date = '2524608000'; //posts without an expiration date show up until 2050;

} else {

$expirestringarray = implode($expirationdate);

$expiration_date = strtotime($expirestringarray);

}

if ($expiration_date > $today) { $valid = "yes"; } else { $valid = "no"; } 

if ( $valid == "yes" ) { ?>

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

<?php } //end if for expiration; ?>

Initial URL


Initial Description
This is an updated version of the original Wordpress post expiration code (<a href="http://snipplr.com/view/3899/wordpress-post-expiration-code/">3899</a>) that bugs out towards the end/beginning of a month when using European date format (d.m.Y). Usage is the same, this code makes "expired" posts cease to show up in the normal page, but they should still show up in the archives. When writing the post, enter a custom field with the key "expiration" and set the date. The below code will go on the index page in the theme folder.

I've changed the way dates are compared using <a href="http://www.highlystructured.com/comparing_dates_php.html"> this article</a> for reference.

Anomalies handling:

- If there is no expiry date set, a post will show up until 2050.
- If the expiration field has a value that can't be converted to Unix timestamp, it will be empty and thus smaller than today's date, i.e. the post won't show up on the home page.

Initial Title
Updated March 2011: Wordpress post expiration code

Initial Tags


Initial Language
PHP