Blog
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.
- 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.
- We need to get the current date to compare the start and end date values against.
<?php $today = date("m/d/Y"); ?>
- 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";
?>
- 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 );
?>
- 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] );
?>
- 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 »'); ?>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
<p align="center"><?php next_posts_link('« Previous Entries') ?> <?php previous_posts_link('Next Entries »') ?></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
More Blog Articles
- Outsource Services to Support Business Growth - 08/31/10
- 3 Easy Changes for a Sticky Website -- Web Site Redesign Tips - 08/25/10
- Hiring writers: 5 quick ways to avoid headaches and lawsuits - 08/06/10
- Web traffic: 3 ways to attract customers to your website - 07/23/10
- How do you know if a small business web designer is up to the job? - 07/15/10
- 5 keys to choosing a reputable SEO firm - 06/11/10
- 5 Ways an iPhone App Developer Can Make Your Business Money - 05/25/10
- How is Your Web Strategy? - 03/05/10
- Patience is The Key to Web Success - 03/05/10
- The Website ROI Factor - 02/28/10
- Live Support Chat Software - 02/26/10
- Automate Your Workforce with iPhone - 02/25/10
- Mobile CRM - 02/23/10
- Wordpress for iPhone - 02/22/10
- Mobile Development - 02/19/10
- Coordinates Using the iPhone Simulator - 12/30/09
- Social Media - 12/24/09
- LINQ Likes Foreign Keys - 11/16/09
- Adding a Flickr Slide Show to Your Website - 10/07/09
- Adding a Twitter Feed to Your Site - 09/22/09
- Making Sure Your Website Is Browser Friendly - 09/11/09
- Adding your Facebook Fan Box to Your Website - 08/13/09
- Whats the big deal about BING? - 07/31/09
- Web Design Radio Interview pt 4 - 06/25/09
- Adding a Start Date and End Date to your WordPress Posts - 06/16/09
- Web Design Radio Interview pt 3 - 05/15/09
- Web Design Radio Interview pt 2 - 05/08/09
- Web Design Radio Interview pt 1 - 05/07/09
- Radio Interview - 05/04/09
- Is it Time to Add a Survey Tool to Your Website? - 04/29/09
- Key Step in Building a Website - 04/16/09
- Google Analytics Now Offers iPhone Tracking on Your Website Analytics. - 04/15/09
- Google Video to Discontinue Upload Service - 04/03/09
- LINQ to Objects - 04/01/09
- Sales in a Down Economy - 03/31/09
- Integrating The Facebook Connect Platform Into Your Website - 03/27/09
- Is it Time to Make Sure Your Website is Formatted for Higher Resolution Monitors? - 03/24/09
- Another Reason Reviewing Google Analytics Data is Important. - 03/20/09
- Client-side Reports Using the Report Viewer - 03/18/09
- Targeting the Right Destination in a Hyperlink - 03/17/09
- Is Your Website Multilingual? Should It Be? - 03/12/09
- How to Use Javascript Effectively Without Hurting Your Web Site Search Engine Optimization - 03/05/09
- Top 7 Myths of Web Development - 03/04/09
- Using LINQ - 03/02/09
- Facebook as a Business Tool - 02/27/09
- Proper Keyword Selection for SEO - 02/24/09
- My brother knows a guy who knows a guy who does websites for $500. - 02/23/09
- Maintaining High Touch in a High Tech World - 02/19/09
- Search Engine Optimization And W3C Compliant Web Pages - 02/18/09
- Reach potential clients through RSS - Part 2 - 02/18/09
- Reach potential clients through RSS - Part 1 - 02/17/09
