Monday, June 28, 2010

Show future posts on single post

today i show how to display future posts in single.php. you just copy code below into funciton.php.

add_filter('the_posts', 'show_future_posts');

function show_future_posts($posts)
{
global $wp_query, $wpdb;

if(is_single() && $wp_query->post_count == 0)
{
$posts = $wpdb->get_results($wp_query->request);
}

return $posts;
}

Displaying Future Posts in WordPress

If you do like I do and schedule posts ahead of time (especially on the podcast), there’s a way inside of WordPress to show your readers what posts are coming up next:

<?php
$my_query = new WP_Query('post_status=future&order=DESC&showposts=5');
if ($my_query->have_posts()) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php the_date(); ?> - <?php the_title(); ?>
<?php endwhile;
}
?>


This will display up to 5 “scheduled” posts wherever you drop in the code. Add a headline <h2> tag if you want to give it a title (should you want to use it as a widget).

Want to see this code in action? Check the WordPulse podcast page‘s sidebar to see it working!

Display a List of Upcoming Posts in WordPress

When you have completed a post in WordPress, you have the ability to save it as a draft, publish it immediately or schedule a future date when it will be published automatically.

This is extremely helpful if you want to plan ahead or if you use your WP site for events. Creating a simple list of upcoming posts lets your visitors know what they can look forward to and is pretty simple to do.




The following code can be placed in your sidebar on within any
template.
<?php
$futurePosts = new WP_Query();
$futurePosts->query('showposts=5&post_status=future&order=ASC');

if ($futurePosts->have_posts()) : while ($futurePosts->have_posts()) :
?>
<ul>
<li><?php the_title(); ?><br /><small><?php the_time(get_option('date_format'); ?></small></li>
</ul>
<?php endwhile; else: ?>
<ul>
<li>No upcoming events.</li>
</ul>
<?php endif; ?>

If you want to only display future posts from a specific category, you

$futurePosts->query('showposts=5&post_status=future&order=ASC&cat=12');

can add a cat=$id to the query like so: