Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the following code to display posts with todays date or in the future... except the future part isn't working. If I take out the if statement then it shows all posts - in the past, present and future.

$today = date( 'd M' ); 
$pages = get_children( array(
    'post_status' => 'future,publish',
    'post_type' => 'event',
    'sort_order' => 'DESC'
) );
foreach ( $pages as $post ) {
    setup_postdata($post);
    $item = get_the_date(); 
    if ($item >= $today) { 
        ?><h3><?php echo the_title(); ?> - <?php the_date(); ?></h3><?php
    }
}
wp_reset_query();

How can I display posts with todays date and posts in the future?

share|improve this question
add comment

3 Answers

Hook posts_where filter and add your condition to the sql query:

function wpse8170_filter_where( $where = '' ) {
    $where .= sprintf( " AND post_date >= ''", date( 'Y-m-d' ) );
    return $where;
}

// add your hook
add_filter( 'posts_where', 'wpse8170_filter_where' );

// get pages
$pages = get_children( array(
    'post_status' => 'future,publish',
    'post_type' => 'event',
    'sort_order' => 'DESC'
) );

// remove your hook to not have influence on other get posts queries
remove_filter( 'posts_where', 'wpse8170_filter_where' );

foreach ( $pages as $post ) {
    setup_postdata($post);

    ?><h3><?php echo the_title(); ?> - <?php the_date(); ?></h3><?php
}
wp_reset_query();
share|improve this answer
add comment

Use get_posts function with WP_Query params instead of get_children. The code will than look like this:

$today = getdate(); 
$pages = get_posts( array(
    'post_status' => array( 'publish', 'future' ),
    'post_type' => 'event',
    'sort_order' => 'ASC',
    'orderby' => 'date',
    'year' => $today["year"],
    'monthnum' => $today["mon"],
    'day' => $today["mday"]
) );
foreach ( $pages as $post ) {
    setup_postdata($post);
        ?><h3><?php echo the_title(); ?> - <?php the_date(); ?></h3><?php
}

wp_reset_postdata();
share|improve this answer
    
Correct, the post_status can accept an array (or string) as its value. –  Rilwis Jan 30 '13 at 14:34
    
Thanks, and also notice change in wp_reset_query - it transformed into wp_reset_postdata... Read more on wp_reset_query vs. wp_reset_postdata - wordpress.stackexchange.com/questions/8031/… –  david.binda Jan 30 '13 at 14:38
add comment

I figured out what it was doing...

It was measuring the day of the month against each other so I reversed it to Ymd.

share|improve this answer
    
Could you please add your code. Explaining code in plain words often leads to the wrong results. THanks. –  kaiser Jan 30 '13 at 18:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.