Updated March 2011: Wordpress post expiration code


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. ***PUT THIS INSIDE THE LOOP, FIRST THING***
  2.  
  3. <?php //to check against expiration date;
  4.  
  5. $todays_date = date("d.m.Y");
  6.  
  7. $today = strtotime($todays_date);
  8.  
  9. $expirationdate = get_post_custom_values('expiration');
  10.  
  11. if (is_null($expirationdate)) {
  12.  
  13. $expiration_date = '2524608000'; //posts without an expiration date show up until 2050;
  14.  
  15. } else {
  16.  
  17. $expirestringarray = implode($expirationdate);
  18.  
  19. $expiration_date = strtotime($expirestringarray);
  20.  
  21. }
  22.  
  23. if ($expiration_date > $today) { $valid = "yes"; } else { $valid = "no"; }
  24.  
  25. if ( $valid == "yes" ) { ?>
  26.  
  27. ***THEN PUT THE FOLLOWING LINE AT THE VERY END OF THE LOOP***
  28.  
  29. <?php } //end if for expiration; ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.