Posted By

ramborc on 03/04/11


Tagged

posts


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

ramborc
hkamci


Updated March 2011: Wordpress post expiration code


 / Published in: PHP
 

This is an updated version of the original Wordpress post expiration code (3899) 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 this article 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.
  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  

You need to login to post a comment.