BolderImage

Blog Subscribe to our blog Subscribe to our RSS blog feed Bookmark and Share

More Blog Entries




Adding a Start Date and End Date to your WordPress Posts
06/16/09


A feature in WordPress that we have found to be lacking is the ability to set a Start Date and an End Date for a post.  It’s reasonable to think that I might want to pre-populate a site with some pages and have them automatically be available on a certain date.  Conversely, I’d like the ability to automatically have content expire based on the date.  Since we haven’t found a great solution for this, we developed a resolution ourselves.  This post will show how we easily added that ability to WordPress.

The first step is to add custom data fields “StartDate” and “EndDate” to your post.  The values need to be in mm/dd/yy or mm/dd/yyyy format.  You can do that by clicking on the ENTER NEW link in the CUSTOM FIELDS section.  Add a field name of “StartDate” and then a value and click on ADD CUSTOM FIELD.  Do the same for the “EndDate”.

 

Now, you’re ready to start changing your theme.

 

  1. Locate where we loop through the posts.  In most cases, you’ll see code like:
<?php while (have_posts()) : ?>
<?php the_post(); ?>
We'll want to put our new code right after this section.
  1. We need to get the current date to compare the start and end date values against.
<?php $today = date("m/d/Y"); ?>
  1. Next, we need to get the start and end date values from the post.  We need to make sure that we get values back and if we don’t, we need to set the start date to an arbitrary low value and the end date to an arbitrary high value so that a post that doesn’t have a start date and end date should always be on.

<?php
$mystartdate = get_post_meta($post->ID, 'StartDate', true);
if ( $mystartdate == "" )
     $mystartdate = "01/01/2000";

$myenddate = get_post_meta($post->ID, 'EndDate', true);
if ( $myenddate == "" )
     $myenddate = "01/01/2032";
?>

  1. Now, here is where it gets a little tricky.  You cannot just compare the values of the start and end dates that we pulled from the post to the current date.  We have to convert them to a different type of variable using mktime.  However, before we can do that, we have to pull the month, day and year from the date values.

<?php
$starttime = explode( "/", $mystartdate );
$todaytime = explode ("/", $today );
$endtime = explode ("/", $myenddate );
?>

  1. Now that we have broken out the month, day and year values into an array, we can now convert that to a new variable using mktime.
<?php
$testtime1=mktime( 0, 0, 0, $starttime[0], $starttime[1], $starttime[2] );  
$new_today=mktime( 0, 0, 0, $todaytime[0],$todaytime[1],$todaytime[2] );
$testtime2=mktime( 0, 0, 0, $endtime[0], $endtime[1], $endtime[2] );
?>
  1. We have all the variables in place so all we have to do now is add a check to see if the current date falls in the acceptable date range for that post.  You can do that by adding the following if condition:

 

<?php if (($new_today>=$testtime1)&&($new_today<=$testtime2)) : ?>

…and there you have it.  You can now control start date and end date for your posts.  For convenience, here’s a full example of a modified index.php:

 

 

<?php get_header(); ?>
<div id="content">
    <div id="main">
    <?php if (have_posts()) : ?>
<?php while (have_posts()) : ?>
<?php the_post(); ?>
<?php

$today = date("m/d/Y");

$mystartdate = get_post_meta($post->ID, 'StartDate', true);
if ( $mystartdate == "" )
     $mystartdate = "01/01/2000";

$myenddate = get_post_meta($post->ID, 'EndDate', true);
if ( $myenddate == "" )
     $myenddate = "01/01/2032";

$starttime = explode( "/", $mystartdate );
$todaytime = explode ("/", $today );
$endtime = explode ("/", $myenddate );

$testtime1 = mktime( 0, 0, 0, $starttime[0], $starttime[1], $starttime[2] );  
$new_today = mktime( 0, 0, 0, $todaytime[0], $todaytime[1], $todaytime[2] );
$testtime2 = mktime( 0, 0, 0, $endtime[0], $endtime[1], $endtime[2] );

?>

<?php if (($new_today>=$testtime1)&&($new_today<=$testtime2)) : ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>

<div class="entry">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>
</div>
<?php endif; ?>

<?php endwhile; ?>

<p align="center"><?php next_posts_link('&laquo; Previous Entries') ?> <?php previous_posts_link('Next Entries &raquo;') ?></p>
<?php else : ?>
<h2 align="center">Not Found</h2>
<p align="center">Sorry, but you are looking for something that isn't here.</p>
    <?php endif; ?>
    </div>
<?php get_sidebar(); ?>

</div>
<?php get_footer(); ?>

</body>
</html>

- Dave Gruen
Senior Vice-President, Consulting Division

Tags: WordPress tech 

Leave a Comment