Display Future Posts in Your WordPress Theme

Display Future Posts in Your WordPress Theme

10. Oktober 2008 in WordPress Funktionen 28 Kommentare

Today I want to focus on a very interesting feature of WordPress – the timestamp of a post or how to set a post on schedule and – and what else can we do with future posts. Learn how to run your blog on autopilot.

Maybe this happened to you once

All of a sudden you have a lot of inspiration and ideas for posts on your blog. Your head is full with first class content :-) and you want to write 5 posts… one after another. But since we all know that publishing all these posts at once is a waste of energy, we decide to spread the new content across several days or even weeks.

Or maybe you go on vacation for four weeks (lucky you!) but you want to keep offering your ‘Friday Web Design Blog Review’.

Wouldn’t it be nice to run your blog on autopilot?

To set a post on schedule to be publihed on a certain date at a certain hour is quite easy. In the write post panel in the right sidebar you’ll see the ‘Publish Status’ box. Under ‘Keep this post private’ it says ‘Publish immediately’. Click on ‘Edit’ next to it.

Display Future Posts in Your WordPress Theme

A form opens up where you can change the timestamp of the post. Here you can set the exact date and hour when you want you post to be published. If you choose a future date your post will appear as scheduled in the manage posts overview.

Your blog runs on autopilot now!

What else can we do with this timestamp feature?

Now we know that we can save posts with a future date. But can we display them on our blog? Yes, we can. And because we can, I had the idea of building an Upcoming Events list like in the example below.

Display Future Posts in Your WordPress Theme

In my example we have a list with upcoming events and onther list below with previous events. Please note that the upcoming events list is in chronological (ascending) order. Once a future event date passes, it will automatically change to the Previous Events list.

First I want to show you how to call the previous events. Since these are normal posts, we just make a normal query for posts of a certain category – here events. If there are no previous events, the phrase No previous Events will be displayed.

<h4>Previous Events</h4>
 
<?php
$my_query = new WP_Query('category_name=events');
?>
 
<?php
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$do_not_duplicate = $post->ID;
?>
 
<ul>
  <li>
    <strong><?php the_time('l, F jS, Y') ?>:</strong><br />
    <?php the_title(); ?>
  </li>
</ul>
 
<?php endwhile; else: ?>
 
<ul>
  <li><?php _e('No previous Events'); ?></li>
</ul>
 
<?php endif; ?>

Now we try to display the future posts just adding some arguments to the post query.

<h4>Upcoming Events</h4>
 
<?php
$my_query = new WP_Query('category_name=events&post_status=future&order=ASC');
?>
 
<?php
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$do_not_duplicate = $post->ID;
?>
 
<ul>
  <li>
    <strong><?php the_time('l, F jS, Y') ?>:</strong><br />
    <?php the_title(); ?>
  </li>
</ul>
 
<?php endwhile; else: ?>
 
<ul>
  <li><?php _e('No upcoming Events'); ?></li>
</ul>
 
<?php endif; ?>

Please note that the WP_Query function now only asks for posts with post_status future and will display them in an ascending (chronological) order. If there are no upcoming events, the phrase No upcoming Events will be displayed.

Put it all together

In my example I want to display the upcoming events on the top and the previous events below. So here you get the complete code:

<h4>Upcoming Events</h4>
 
<?php
$my_query = new WP_Query('category_name=events&post_status=future&order=ASC');
?>
 
<?php
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$do_not_duplicate = $post->ID;
?>
 
<ul>
  <li>
    <strong><?php the_time('l, F jS, Y') ?>:</strong><br />
    <?php the_title(); ?>
  </li>
</ul>
 
<?php endwhile; else: ?>
 
<ul>
  <li><?php _e('No upcoming Events'); ?></li>
</ul>
 
<?php endif; ?>
 
 
<h4>Previous Events</h4>
 
<?php
$my_query = new WP_Query('category_name=events');
?>
 
<?php
if ($my_query->have_posts()) : while ($my_query->have_posts()) :
$my_query->the_post();
$do_not_duplicate = $post->ID;
?>
 
<ul>
  <li>
    <strong><?php the_time('l, F jS, Y') ?>:</strong><br />
    <?php the_title(); ?>
  </li>
</ul>
 
<?php endwhile; else: ?>
 
<ul>
  <li><?php _e('No previous Events'); ?></li>
</ul>
 
<?php endif; ?>

Now just style the list to your liking and you have a nice automatic events list divided into upcoming and previous event.

Simon [webdemar]

Moin, ich bin Simon und betreibe webdemar.com als meine ernstgemeinte Spielwiese für WordPress, Webdesign und so.

RSS abonnieren Twitter

24 Kommentare

  • Ben
    11. Oktober 2008

    Thanks dude! The events list is a great use of this Wordpress feature!

  • Free Gadgets
    16. Oktober 2008

    Great article. I could really use the information here. I had been looking for this sort of thing for a while. Thanks

  • Garritt Hampton
    20. Oktober 2008

    Great tip! I was checking out your themes (which are gorgeous, by the way), when I found this post. Very timely. I put it to work right away on my own blogs, then I published a similar tip on my site, with a link back here, of course. I didn’t go into the depth that you did. I figured it people wanted to go that deep, they could learn that here. Nice job.

  • Brandon
    24. Oktober 2008

    What a great tutorial – I’ve been looking for something like this for a year now – thanks!

  • Gadget Tips
    28. Oktober 2008

    I don’t want to repeat what other have said but this is a great article. I would have to try this.

  • Keith Millington
    3. November 2008

    Hmm, looks mighty similar to the code posted here back in June …
    http://www.keithmillington.co.uk/wordpress/?p=22

    :)

  • Shareen McCaffrey
    29. November 2008

    I love this idea. I am going to use your idea for my membership site (I created using wordpress).

  • Vasile Filat
    19. Dezember 2008

    Thank you very much. I was looking for a long time after such an advice and I already use this on my romanian part of the blog and also, wrote an article in Romanian so that people may use it. Thank you very much and may God bless you with a blessed Christmas and with the gift of Eternal Life through faith in our Lord Jesus Christ.

  • Gabe Diaz
    21. Dezember 2008

    Wow, never thought/knew of “post_status=future” to display future scheduled posts. This is a great tip!

  • Ben T
    20. Januar 2009

    Thanks so much! This is such a great function

  • MrLive
    30. Januar 2009

    Great code! thanks.
    At first, mine not work until I remove the category=event ^^ and then I develop it into a plugin with some advanced feature :)
    http://wordpress.org/extend/plugins/upcoming-posts/
    enjoy!

  • David
    27. Februar 2009

    Great use of post status! I like the simplicity of idea rather than adding massive calendar plugin

  • Sabrina
    20. April 2009

    I am using an upcoming posts list in my footer and the text formatting is not the same as the rest of my themes for some reason…how do I hcange it to look like the rest of the footer’s font size/color? And how do I make each future post list as a new list item?

    Thanks for the help,
    Sabrina

    • Simon [webdemar]
      20. April 2009

      @Sabrina:
      At the moment you have a php error in your footer. To help you fix your problem I need to see it live.

  • Karel Zeman
    30. April 2009

    Excellent tip there, there’s nothing better than scheduling posts in advance, especailly with news. It’s worth spending a good few hours to write some posts at the start of the month and spread them out throughout the month.

  • Naif Amoodi
    28. Juli 2009

    Alternatively you can opt on using the following plugin which does just that: http://wordpress.org/extend/plugins/display-scheduled-posts/

  • peter
    28. September 2009

    Funktioniert gut, nur die Standartsuche von WordPress sucht nicht in Artikeln mit zukünftigen Datum. Das ist doof. Das Plugin “The Future Is Now” ermöglicht ebenfalls im Loop future Posts anzuzeigen, diese werden aber auch mit der Suchfunktion gefunden. Naürlich ist eine Lösung ohne Plugin immer vorzuziehen. Vielleicht kannst Du das Problem ja noch lösen!?

  • Emma
    30. September 2009

    Hi! Great tips!
    Wonder if there is any way to have the future events posted in the feed as well? Today I only see the previous..

    Big thanx for any ideas to solve the problem :)

    /Emma

  • Nick
    20. November 2009

    You need to add &posts_per_page=-1 if you want to show all future events. Otherwise it will limit it to whatever your normal page count is.

  • Joey
    24. November 2009

    Dude! Sweet!

  • Stanley Dumanig
    5. Dezember 2009

    I have the same problem on future posts not displaying on single post template(single.php) but I created my own plugin to fix this. NO hacks and NO need to edit your templates. Just install it and it solves the problem.

    Check: http://wordpress.org/extend/plugins/show-future-posts-on-single-post/

    Running example is at http://www.FredRadio.com

  • Bahattin ARICI
    20. Dezember 2009

    Thank you very much for the code. I worked a lot of work.
    Translate Turkish: Kodlar için çok teÅŸekkürler. Çok iÅŸime yaradı.

  • Andrej
    10. März 2010

    Thank you. I however ran into a problem I can’t solve. I can only access future posts when logged in. When a guest user comes he can only access Past posts, can see the list of future posts but the future posts appear as 404 to him. Any suggestions?

4 Trackbacks

Kommentar schreiben

Dein Name hier
Dein Name hier
11. March 2010

Gravatars sind an. Jetzt kostenlos registrieren!

WordPress Hosting

Für das Hosten von WordPress Blogs empfehlen wir Webspace von Hostloco.
Eine ganze Fotowelt mit nur einer Software - Fotobuch-Software von CEWE.

So am Rande

WordPress 2.8.6 Security Release - 16. November 2009

WordPress versorgt uns von der nächsten großen Version 2.9 noch mit einem weiteren Sicherheits-Release – WordPress 2.8.6. Damit wird eine [...]

WordPress 2.8.5 schließt Sicherheitslücke - 21. Oktober 2009

Seit heute Nacht ist WordPress 2.8.5 zum Download erhältlich. Obwohl schon alle Maschinen für die Version 2.9 laufen, schließt WordPress [...]

WordPress 2.9 kündigt sich an - 14. Oktober 2009

Bald ist es soweit und die erste Beta-Version von WordPress 2.9 wird zum Download erhältlich sein. Das soll laut Peter [...]

Link: Anleitung zur Erstellung von Theme-Options in einem WordPress-Theme - 5. Oktober 2009

Wer ein professionelles WordPress-Theme erstellen möchte, kommt um Theme-Options, einer separaten Seite für Theme-Einstellungen im WordPress-Admin, nicht herum. Theme-Options bieten [...]

Link: Digging into WordPress - 1. Oktober 2009

Sollte es unter Euch einen WordPress-Begeisterten geben, der diese Seite noch nicht kennt, möchte ich ihm diese hiermit vorstellen. Auf [...]

Kostenloser Mozilla Firefox Download des aktuellen Mozilla Firefox Browser.
Offizielles Stadtportal für München. Ein Service für die Landeshauptstadt München.
Do you like this theme?

Powered by WordPress.org - Copyright © 2009. Alle Rechte vorbehalten - Professional WordPress Themes