Support » Fixing WordPress » Displaying Future/Scheduled Posts

  • I am using this code:

    <?php
    	  $now = current_time('mysql');
    	  $future_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_date > '$now' ORDER BY post_date ASC");
    
    	  if($future_posts) : foreach($future_posts as $post) : setup_postdata($post);
    
    	  ?>

    from this post: http://wordpress.org/support/topic/displaying-futurescheduled-posts

    this works great and displays all future posts… I want to limit the posts to a set number such as 8. How would i go about this?

Viewing 13 replies - 1 through 13 (of 13 total)
  • Never had reason to do this before, but I’d be inclined to try simplify by swapping out the $wpdb->get_results bit for get_posts.

    Perhaps something along these lines (fair warning– your mileage may vary)?

    <?php
    	$args = array(
    		'orderby' => 'post_date',
    		'numberposts' => 8,
    		'post_status' => 'future'
    	);
    
    	$future_posts = get_posts( $args );
            if($future_posts) : foreach($future_posts as $post) : setup_postdata($post);
    
    ?>

    thanks andrew for your response.

    I had found a heap of solutions using ‘post_status’ => ‘future’, however i am using the “the future is now” plugin which removes the “future” state…so this solution will not work..

    I see. Not familiar with that plugin, but I understand the concept.

    Have you tried adding/using a TOP clause (LIMIT in MySQL) to your SQL statement?

    i am fluent with html/css/wp themes… i am bit a hack with the php.. this recommendation is beyond my understanding….

    Forgive me, I was in a rush this morning and should have provided more detail. Mea culpa.

    Something like this:

    $future_posts = $wpdb->get_results("SELECT LIMIT 8 * FROM $wpdb->posts WHERE post_date > '$now' ORDER BY post_date ASC");

    thanks for your time Andrew.. this code still not working

    Hmm. It’s definitely a MySQL database, right?

    Does it give any error message? Or is it still returning results, and just giving you back more than 8 records?

    Moderator keesiemeijer

    (@keesiemeijer)

    See time parameters: http://codex.wordpress.org/Function_Reference/WP_Query#Time_Parameters

    Try it with a loop like this:

    <?php
    global $wp_query;
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    
    $args = array_merge( $wp_query->query_vars, array(
    		'posts_per_page'  => 8, // show 8 posts
    		'paged'           => $paged
    	) 
    
    );
    // add a filter to show future posts only
    add_filter( 'posts_where', 'filter_where' );
    $the_query = new WP_Query( $args );
    // remove the filter
    remove_filter( 'posts_where', 'filter_where' );
    ?>
    <?php 
    
    // the loop
    if ( $the_query->have_posts() ):
    	while ( $the_query->have_posts() ) :
    		$the_query->the_post(); ?>
    <!-- your loop code here -->
    <?php the_title() ?>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    <?php else : ?>
    
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    
    <?php endif; ?>

    And with this in your theme’s functions.php:

    function filter_where( $where = '' ) {
    	// posts in the future
    	$where .= " AND post_date > '" . date('Y-m-d', strtotime('now')) . "'";
    	echo $where;
    	return $where;
    }

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost.

    ahh sorry andrewmills.. bad response… the code did not work as it did not display anything. no error message

    yes using mysql

    thank you keesiemeijer… this code returned

    AND wp_posts.ID = 131 AND wp_posts.post_type = 'post' AND post_date > '2013-03-05'
    
    Sorry, no posts matched your criteria.

    i also trie dthis code with a <?php wp_reset_query(); ?> to ensure it was not conflicting with the main loop

    Moderator keesiemeijer

    (@keesiemeijer)

    If this is a secondary loop you can leave out the original query (wp_posts.ID = 131).
    Try changing this:

    $args = array_merge( $wp_query->query_vars, array(
    		'posts_per_page'  => 8, // show 8 posts
    		'paged'           => $paged
    	) 
    
    );

    to this:

    $args = array(
    		'posts_per_page'  => 8, // show 8 posts
    		'paged'           => $paged
    	);

    leave the <?php wp_reset_query(); ?> in there to not conflict with the original loop.

    thankyou keesiemeijer… this is working a bit better… the posts are not showing the next ones.. they are about a month ahead.. also this code is displaying above the posts: AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND post_date > '2013-03-06'

    Moderator keesiemeijer

    (@keesiemeijer)

    What exactly isn’t working? Remove this to get rid of the text: echo $where;

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Displaying Future/Scheduled Posts’ is closed to new replies.