How to set expiration date for a post using custom fields?

Custom fields provide  a strong tool for developers to make it more user friendly and customizable.

Today, we will see how we can assign an expiration date to a post, so the post will disappear after a particular date:

1)      Change the default loop in your index.php file as mentioned below:
 

<?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 ) {
// For exemple…
the_title();
the_excerpt();
}
endwhile;
endif;
?>

2)      Now, when you add/edit a post, add a custom field expiration and assign the date as value(with the format mm/dd/yyyy 00:00:00). The post will disappear after that date from the home page.

Note that if you do not assign the custom field to a post, that post will not appear on your home page. This code needs some modification for the same.

You may like these articles :

  1. Introduction to WordPress Database: wp_postmeta Introduction to WordPress Database: wp_postmeta Current Version : 2.9.2 Just...
  2. Using gravatar for the post author in wordpress posts Using gravatar for the post author in wordpress posts: We...
  3. Delicious starts using Twitter to rank bookmarks on home page Delicious, the Yahoo-owned social-bookmarking site, has awoken from its slumber,...

-

One Comment


  1. Jesse Rosenfield
    Mar 17, 2010

    There is no code displaying at “1)” where it says Change the default loop in your index.php file as mentioned below:

Leave a Reply