logo

$10
WP Query only future posts

I am building a simple calendar for a client. I used the No Future Posts Plugin to enable me to publish posts whose date are set for a future date.

My problem is now, that in this list of posts, I'd like to be able to show only posts today or in the future, no past posts so it's only upcoming events. Is this possible? How might I modify the wp_Query below to accomplish this?

<?php $my_query = new WP_Query( array( 'post_type'=>'post', 'posts_per_page' => 6, 'order' => 'ASC', 'cat' => 5 ) );while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID; ?>
<li><a href="<?php the_permalink() ?>"><?php the_date( 'n/d'); ?><?php the_title(); ?></a></li>
<?php endwhile; ?>

Jesse Sutherland | 07/20/10 at 5:07pm | Edit


(5) Possible Answers Submitted...

  • avatar
    Last edited:
    07/20/10
    5:12pm
    Utkarsh Kukreti says:

    Use

    Today's posts


    $today = getdate();
    $my_query = new WP_Query( array( 'post_type'=>'post', 'posts_per_page' => 6, 'order' => 'ASC', 'cat' => 5, 'year' => $today["year"], 'monthnum' => $today["mon"], 'day' => $today["mday"] ) );


    Future posts

    $my_query = new WP_Query( array( 'post_type'=>'post', 'posts_per_page' => 6, 'order' => 'ASC', 'cat' => 5, 'post_status' => 'future') );

    Previous versions of this answer: 07/20/10 at 5:12pm

  • avatar
    Last edited:
    07/20/10
    5:12pm
    Oleg Butuzov says:

    just filter query... add post_date > NOW() for that

    • 07/20/10 5:13pm

      Oleg Butuzov says:

      as sample of query parse (btw its a bit related to your issue)

      http://wpquestions.com/question/show/id/485

    • 07/20/10 5:27pm

      Oleg Butuzov says:

      oh... take a look to the solution posted wjm. same as i tald. and its perfectly fit your needs.

  • avatar
    Last edited:
    07/20/10
    5:13pm
    Pippin Williamson says:

    Take a look at these links:

    http://lorelle.wordpress.com/2006/10/01/working-ahead-future-posts-with-wordpress/

    http://www.studionashvegas.com/tutorial/displaying-future-posts-in-wordpress/

    http://wordpress.org/extend/plugins/display-future-posts/

  • avatar
    Last edited:
    07/21/10
    10:02am
    wjm says:

    hi,
    your query should look like

    new WP_Query( array( 'post_type'=>'post', 'posts_per_page' => 6, 'order' => 'ASC', 'cat' => 5, 'post_status' => 'publish,future', ) );


    and based on
    http://codex.wordpress.org/Template_Tags/query_posts#Time_Parameters
    you can apply a filter (i modified the code to suit your needs)

    <?php
    //based on Austin Matzko's code from wp-hackers email list
    function filter_where($where = '') {
    $where .= " AND post_date >= DATE( NOW() )";
    return $where;
    }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    ?>


    so you will end up with something like this,



    <?php
    //based on Austin Matzko's code from wp-hackers email list
    function filter_where($where = '') {
    $where .= " AND post_date >= DATE( NOW() )";
    return $where;
    }
    add_filter('posts_where', 'filter_where');
    $my_query = new WP_Query( array( 'post_type'=>'post', 'posts_per_page' => 6, 'order' => 'ASC', 'cat' => 5, 'post_status' => array( 'publish', 'future' ), ) );while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID; ?>

    <li><a href="<?php the_permalink() ?>"><?php the_date( 'n/d'); ?><?php the_title(); ?></a></li>

    <?php endwhile; ?>

    <?php
    remove_filter('posts_where', 'filter_where');
    ?>


    I will try this code in my site and submit any improvement as i havent run it yet.

    Previous versions of this answer: 07/20/10 at 5:39pm

    • 07/20/10 5:39pm

      wjm says:

      ok. this is the revised version as the above had a little error,


      <ul>
      <?php
      function filter_where($where = '') {
      $where .= " AND post_date >= DATE( NOW() )";
      return $where;
      }
      add_filter('posts_where', 'filter_where');
      $my_query = new WP_Query(
      array(
      'post_type'=>'post',
      'posts_per_page' => 6,
      //'orderby' => 'date',
      'order' => 'ASC',
      'cat' => 5,
      'post_status' => 'publish,future',
      )
      );
      while ( $my_query->have_posts() ) :
      $my_query->the_post();
      $do_not_duplicate = $post->ID;
      ?>
      <li><a href="<?php the_permalink() ?>"><?php the_date( 'n/d'); ?> <?php the_title(); ?></a></li>
      <?php
      endwhile;
      remove_filter('posts_where', 'filter_where');
      ?>
      </ul>



      and it works.
      Note: the above code lists today's posts (they could have been published a couple of hours ago today and they will be still showed).
      if you just want future, change
      $where .= " AND post_date >= DATE( NOW() )";
      to
      $where .= " AND post_date >= NOW()";

      you may want to uncomment
      //'orderby' => 'date',
      but i think posts are sorted by date by default.

      let me know if you have any questions
      - wjm

  • avatar
    Last edited:
    07/20/10
    9:29pm
    Darrin Boutote says:


    <?php
    $my_query = new WP_Query( array(
    'post_status' => 'future,publish',
    'post_type'=>'post',
    'posts_per_page' => -1,
    'order' => 'ASC',
    'cat' => 5 )
    );

    if ( $my_query->have_posts() ) :

    while ($my_query->have_posts()) : $my_query->the_post();
    if( strtotime($post->post_date) < time() ) {
    //do nothing
    } else { ?>
    <li><a href="<?php the_permalink() ?>"> <?php the_date( 'n/d'); ?> <?php the_title(); ?></a></li>
    <?php } ?>

    <?php endwhile; ?>
    <?php endif; ?>

This question has expired.





Current status of this question: Completed