/ 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!
Expand |
Embed | Plain Text
***PUT THIS INSIDE THE LOOP, FIRST THING*** <?php //to check against expiration date; $expirationdate = get_post_custom_values('expiration'); $expirestring = '30005050'; //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP; } else { } } //else if ( $expirestring > $currentdate ) { ?> ***THEN PUT THE FOLLOWING LINE AT THE VERY END OF THE LOOP*** <?php } //end if for expiration; ?>
Comments

You need to login to post a comment.
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-
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.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.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?
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.
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
pOOP! the code didn't show up. Sorry!
Here tiz:
Could u help me to display posts based on the EXPIRATION date, order=ASC
thanks.baa.
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.
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
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
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 ?
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/
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/