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.
In 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.







Leave a Reply