Ever wanted a feature in Wordpress which lets you set expiration date and time for your posts? Unfortunately Wordpress is missing this thing, however with following two code snippets you can set Post expiration either using only date or using both date and time.
Set Wordpress Post Expiration with Date and Time
Open index.php file of your theme and replace the existing loop with:
<?php
if (have_posts()) :
while (have_posts()) : the_post();
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// Enter your post display code after that.
the_title();
the_excerpt();
// Post display code ends here.
}
endwhile;
endif;
?>
Now while creating a post to set it’s expiration date/time create a custom field with name(key) expiration and set your expiration time as value in this format mm/dd/yyyy 00:00:00.
Source: [WPRecipes]
Set Wordpress Post Expiration with Date
Open index.php file of your theme and replace the existing loop with:
<?php
if (have_posts()) :
while (have_posts()) : the_post();
$currentdate = date("Ymd");
$expirationdate = get_post_custom_values('expiration');
if (is_null($expirationdate)) {
$expirestring = '30005050'; //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP;
} else {
if (is_array($expirationdate)) {
$expirestringarray = implode($expirationdate);
}
$expirestring = str_replace("/","",$expirestringarray);
} //else
if ( $expirestring > $currentdate ) {
// Enter your post display code after that.
the_title();
the_excerpt();
// Post display code ends here.
}
endwhile;
endif;
?>
Now while creating a post to set it’s expiration date create a custom field with name(key) expiration and set your expiration date as value in this format yyy/mm/dd.
Source: [Snipplr]