• dvize

    (@dvize)


    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: https://www.ads-software.com/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)
  • Andrew Mills

    (@andrewmills)

    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);
    
    ?>
    Thread Starter dvize

    (@dvize)

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

    Andrew Mills

    (@andrewmills)

    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?

    Thread Starter dvize

    (@dvize)

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

    Andrew Mills

    (@andrewmills)

    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");

    Thread Starter dvize

    (@dvize)

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

    Andrew Mills

    (@andrewmills)

    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: https://codex.www.ads-software.com/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.

    Thread Starter dvize

    (@dvize)

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

    yes using mysql

    Thread Starter dvize

    (@dvize)

    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.

    Thread Starter dvize

    (@dvize)

    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.