How to Time Delay Posts in WordPress

One reason I’ve heard people say that they prefer a full-blown CMS to WordPress is that WordPress doesn’t allow time delayed posting. I was about to figure out a way to mod WordPress to make this possible – until I realized that there is some decent built-in functionality for time delaying.

You can easily delay the release of your posts into the main indexes (front page, archives, “Recent Posts” list, etc). With a little bit of code, you can also restrict people from viewing the page before its appointed “Published” date.

Delaying Publication: Setting a Date

If all you want to do is delay the date and time that an article is “released” on your page’s index, you don’t have to do a whole lot of work.

Every article you post is “stamped” with a time by WordPress. When you hit the “Publish” button, it checks the current time on the server and attaches that to your post.

You may not have noticed, but there’s an option for you to edit this time while you are working on the article. For example, you could set the date back a week and backdate one of your posts. For our purposes, we’ll set the date ahead – and WordPress won’t display your post in any indexes until that date arrives.

Screenshot of the Edit Timestamp WidgetIn the “Write” section of your Dashboard, you should have a column of widgets on the righthand side (Categories, Discussion, Post Slug, etc). If you scroll down a bit, there should be one titled “Post Timestamp.” If you don’t see anything, click the “+” button in the title.

To set a custom release date, select “Edit Timestamp.” Then use the inputs to enter the date and time you want the article released. Voila – you’re done.

Restricting Access to the Page

For most people, this is good enough. Your readers aren’t going to be able to find the article without a link to it appearing on your front page or in your RSS feed.

However, if your readers do know the link to the page, somehow, they can still access it directly. If you want to prevent that, you’ll need to add a few lines of code to your template files.

We’re going to edit the “single.php” template file – the file responsible for formatting and displaying a single post. After the page retrieves the post, we’re going to perform a check to see if the published date of the article is in the future.

If it is, we’ll display an alternative message to tell the user the article isn’t published yet. Otherwise, the article will be displayed as normal.

Editing Single.php

Your template file probably doesn’t look exactly like mine, but there should be some very basic similarities.

If you open single.php, the page is displayed within “the loop.” It’s how every WordPress template works. The loop always begins with a line similar to this.

<?php while (have_posts()) : the_post(); ?>

This checks to see if a page was found in the database. If it was, it loads the page into memory and prepares to output it. The loop will eventually end with a line like this.

<?php endwhile; ?>

We’re going to create a new if statement and place it in between these two lines. The new template will look something like this.

<?php while (have_posts()) : the_post(); ?>
<?php if ($pubDate > time()) : ?>
  <h3>Article Not Published Yet</h3>
  <p>Sorry!  The article you request is not published 
    yet.  It should be available soon, so check back in a few days.</p>
<?php else : ?>
  ...  Regular post processing goes here ...
<?php endif; ?>
<?php endwhile; ?>

Creating the Conditional Statement

The only thing we have to do now is create a functioning conditional statement that checks to see if the published date of the article is in the past or the future.

To do that, we’ll use a few functions – get_the_time (a WordPress function to get the post’s timestamp), strtotime (a PHP function to convert a date string to a timestamp), and time (a php function to get the current tie).

get_the_time doesn’t return the actual time stamp – it returns a formatted date string, like “March 9, 2008, 11:13:35.” You can specify how it’s formatted, just like you would with PHP’s date function.

To get the timestamp, we’re going to format the time string in a special way so that strtotime() can convert it back to a full timestamp (preserving the exact time the article should be published). Then we compare that timestamp to the system time and see if the date has passed or not.

Use this if statement in the code above, and you should be good to go.

<?php if (strtotime(get_the_time('F j, Y, H:i:s')) > time()) : ?>

If you time delay an article and someone tries to access the article directly, they will now see your alternate message. You should of course personalize that to something appropriate for your site.

You can also apply the same concept to “Pages” in WordPress. You’ll need to edit the page.php template file and insert the same snippet of code there.

What are you waiting for? Go write some articles for the future.

Bookmark and Share:
  • Digg
  • Furl
  • del.icio.us
  • StumbleUpon
  • MisterWong
  • DZone
  • Technorati

Tags: , , , , ,

11 Comments to “How to Time Delay Posts in WordPress”

  1. Typolight said this on

    Thanks, you nice post that helped me alot.

  2. Survey McDonalds said this on

    Just want to say your article is striking. The clarity in your post is simply striking and i can take for granted you are an expert on this subject. Well with your permission allow me to grab your rss feed to keep up to date with forthcoming post. Thanks a million and please keep up the ac complished work. Excuse my poor English. English is not my mother tongue.

  3. travelay said this on

    This is a cool idea, but is there a WordPress plugin for this? Editing the single.php file scares me a bit!

  4. Alfonso Tuohy said this on

    great post ive found some great video on youtube about it check it out here:

  5. vibram outlet said this on

    your details is really reasonable and unique I very concerned about sports and I believe only sports can make you energetic. I want to buy some sports thing ,will you give some suggestions for me from below websites ?

  6. QLStephen said this on

    Статьи мне понравились, буду постоянным читателем
    с нетерпением жду новых публикаций!

  7. allhermesshop said this on

    allhermesshop hermes items – get great deals on clothing, shoes accessories?allhermesshop hermes brikin 25,allhermesshop hermes birkin 30,allhermesshop hermes birkin 35,allhermesshop hermes birkin 40,allhermesshop hermes handbag,garden party tote ,picotin herpicot cosmetic case,allhermesshop hermes jipsy gypsy allhermesshop hermes belts ,allhermesshop hermes.

  8. Sammy Mosinski said this on

    Wonderful thought. My relatives went throught the exact same when they looked for Wicked Tickets. I said to them to Buy Wicked Tickets from BuyWickedTickets . net

  9. TU said this on

    I precisely desired to thank you very much again. I do not know the things I could possibly have undertaken without the entire tricks documented by you directly on such area of interest. It previously was a very depressing crisis in my circumstances, but coming across the very well-written mode you resolved that made me to leap for happiness. Now i’m grateful for the help and expect you really know what a powerful job you are always doing training many others with the aid of a blog. I know that you’ve never encountered all of us.

  10. larry kuyper advisors mortgage said this on

    Hey, excellent job on writing this Web Cash » Blog Archive » How to Time Delay Posts in WordPress up, I am going to link this from my personal blog!

  11. how to unlock iphone 4.3.5 said this on

    Found Trojan.FakeAV what do I do? unlock iphone 4

Leave a Reply