Posted By

jakepaint on 10/15/07


Tagged

php wordpress events expiring posts


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

antix
themolitor
thesbrown
yankeyhotel
baires


Wordpress post expiration code


 / Published in: PHP
 

URL: http://www.kingsbridgetheatre.org/?cat=5

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 value in the format "2007/01/01" The below code will go on the index page in the theme folder. Careful to replace the correct stuff!

  1. ***PUT THIS INSIDE THE LOOP, FIRST THING***
  2. <?php //to check against expiration date;
  3.  
  4.  
  5. $currentdate = date("Ymd");
  6. $expirationdate = get_post_custom_values('expiration');
  7. if (is_null($expirationdate)) {
  8. $expirestring = '30005050'; //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP;
  9. } else {
  10.  
  11. if (is_array($expirationdate)) {
  12. $expirestringarray = implode($expirationdate);
  13. }
  14. $expirestring = str_replace("/","",$expirestringarray);
  15. } //else
  16. if ( $expirestring > $currentdate ) { ?>
  17.  
  18.  
  19. ***THEN PUT THE FOLLOWING LINE AT THE VERY END OF THE LOOP***
  20.  
  21. <?php } //end if for expiration; ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: eagleout on October 22, 2007

Can you give us an example what this code replaces in index.php? Not sure what the "correct stuff" is. I am using the AdClerum template.

Thanks-

Posted By: jakepaint on November 16, 2007

Hi, yeah sure. I changed the code to hopefully be a little more clear. I'm not familiar with AdClerum, but in the loop begins with: <?php if (have_posts()) : while (have_posts()) : the_post(); ?> so put the first section of my code just after that, and the end of the loop is: <?php endwhile; else: so put the last line of my code just before that. My template is a bit different than the standard, so hopefully that will get you working.

Posted By: jakepaint on November 16, 2007

Hi, yeah sure. I changed the code to hopefully be a little more clear. I'm not familiar with AdClerum, but in the loop begins with: <?php if (have_posts()) : while (have_posts()) : the_post(); ?> so put the first section of my code just after that, and the end of the loop is: <?php endwhile; else: so put the last line of my code just before that. My template is a bit different than the standard, so hopefully that will get you working.

Posted By: twitchyjordan on January 24, 2008

Thank you very much for the code. I'm trying to create an events blog using WordPress, and this is very handy for only having current events posted in my "Recently added" column. I've tried adding it to the "Search results" page, and while it works (past events/expired posts won't show up), if there aren't *any unexpired posts the search results end up blank instead of showing up a "no posts found" text. Any thoughts?

Posted By: dwest on February 10, 2008

This is a wonderful snippet. Much needed in WordPress. I have but one request. Can it be adjusted so that a date AND TIME can be specified for expiration?

I run auctions for my art at ebay and I want the blog post to expire when the auction expires. Hours and minutes are all that would be needed along with the date.

If it can be done, and you don't have the time, please tell me how and I'll edit the code.

Thanks so much for creating this and making it available.

Have a look at smallartgems.com to see how I'm using it.

Posted By: dwest on February 20, 2008

I sort of re-wrote this snippet to do two things: It allows one to specify time. It allows one to enter the date as standard U.S.

Your expiration key can now hold the value 02/19/2008 18:25:02 or simlar and the post will expire at that time.

here is the code: PLACE IN WORDPRESS PAGE JUST AS DIRECTED FOR ORIGINAL SNIPPET



//WORDPRESS CODE HERE
//ADD CLOSING  STRING AS DIRECTED IN ORIGINAL SNIPPET.
Posted By: dwest on February 20, 2008

pOOP! the code didn't show up. Sorry!

Here tiz:

Posted By: baa on April 22, 2008

Could u help me to display posts based on the EXPIRATION date, order=ASC

thanks.baa.

Posted By: bujaman on June 20, 2008

Great snippet, how would you modify it so that it would delete the post entirely? I am using WP for a retail site and want to have daily specials on a post. I would like to set them up in advance and set the publish time, but would also like to have them expire and be removed so no one can stumble upon them accidentally in the archives. Thanks for the code.

Posted By: btd1294 on July 2, 2008

I am using the code that you provided on a project that I am working on. It works great, the one thing I need is to be able to sort on the expiration date. Can you give me some help with that?

Thanks for the code.

Jeff

Posted By: jakepaint on July 15, 2008

to sort by expiration date would be on the tricky side. Probably the way would be to set up a custom-select-query in word press. Insert something like this in your template: $querystr = " SELECT wposts.* FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.postid AND wpostmeta.metakey = 'expirationdate' //or whatever your meta key is called AND wposts.poststatus = 'publish' AND wposts.posttype = 'post' ORDER BY wpostmeta.metavalue ASC ";

$pageposts = $wpdb->get_results($querystr, OBJECT);

read more about that method: http://codex.wordpress.org/DisplayingPostsUsingaCustomSelectQuery

Posted By: shopromo on April 11, 2009

Hi, I'd like to use this snippet on a e-couponing website based on Wordpress. But I don't want the expired post to disappear, I just want to add "This coupon code is expired" somewhere in the post when the expiration date is reached. How can I do that ?

Posted By: gjindancer on June 28, 2009

Hi All! I noticed dwest's post has not worked and a solution for including the time in the expiry date was not included.

I've updated the code to include time, account for time differences on your server, and make sure the posts are still included in the archives.

http://snipplr.com/view/16400/updated-wordpress-post-expiration-code/

Posted By: ramborc on March 4, 2011

I've posted an updated version of this code that works with European date format (d.m.Y) at http://snipplr.com/view/50046/updated-march-2011-wordpress-post-expiration-code/

You need to login to post a comment.