1

How would one get posts that are set to 'future' to display in a loop? I am trying to figure out how to get posts that are in the future months. So if we are in April I want to display posts for May. Think of it as a coming up posts.

I have been looking at

http://codex.wordpress.org/Function_Reference/WP_Query#Date_Parameters and I have been looking at this too:

http://joshpress.net/blog/using-new-date-queries-wordpress-3-7/

Something like this but for next month's post:

<?php
//get an array of the date 2 days from now
$twodayslater = getdate( current_time(‘timestamp’) + 2*DAY_IN_SECONDS );
$args = array(
    'date_query' => array(
        'before' => array(
            'year'  => $twodayslater['year'],
            'month' => $twodayslater['mon'],
            'day'   => $twodayslater['mday'],
        ),
        'inclusive' => false,
    ),
    'post_status'         => 'future'
);
$query = new WP_Query( $args );
?>

but have had no luck!

Thanks,

Ben.

0

The Codex link already has anything you seem to be after. Maybe you just didn't know how to use the parameters...

I would do this by means of a date_query:

$args = array(
    'post_type' => 'post',
    'post_status' => array(
        'publish',
        'future',
    ),
    'date_query' => array(
        array(
            'after' => strtotime( 'now' ),
            'before' => strtotime( '+1 month' ),
        ),
    ),
    'posts_per_page' => -1, // or a high number, if you want to pre-fetch post meta data
);
$query = new WP_Query( $args );

while ( $query->have_posts() ) {
    $query->the_post();

    // now you can use the_title(), the_content() etc.
}
wp_reset_postdata();

// EDIT:
If you want to display posts from the (whole) next month only, then you might try the following date query:

$date = strtotime( '+1 month' );
$args = array(
    'post_type' => 'post',
    'post_status' => array(
        'publish',
        'future',
    ),
    'date_query' => array(
        array(
            'year' => date( 'Y', $date ),
            'month' => date( 'n', $date ),
        ),
    ),
    'posts_per_page' => -1, // or a high number, if you want to pre-fetch post meta data
);
  • Thank you for this, I have marked this as the answer. Cheers for all of your help. :) – user3502952 Apr 22 '14 at 18:35
0

You could try:

$args = array(
   'post_type' => 'post',
   'post_status' => 'future',
   'showposts' => -1
);
$posts_array = get_posts( $args );
$this_month = date( 'n' );
$next_month = $this_month + 1;
foreach( $posts_array as $post ): setup_postdata( $post );

    $post_month = get_the_date( 'n' );

    if( $post_month == $next_month ):
        //show what you want about each post
    endif;

endforeach;

Depending on where you want it in your template, you could use WP_Query in stead of get_posts function http://codex.wordpress.org/Class_Reference/WP_Query

If you ask in the comments, I can set up an example of that for you.

  • Hey Douglas, Thanks for this. What I am after is to write a functions / loop that checks the current month and then gets the posts for the next month. So something like this: if is 'Jan' get all posts for 'Feb' else 'Display no post for next month' - Something along these lines! Cheers :) – user3502952 Apr 22 '14 at 17:04
  • Hey again, Thank you for your code. This was great and I have copied it for another project I can use it on. Thank you to all who help :) – user3502952 Apr 22 '14 at 18:36
  • I'm glad you got it sorted out. – Douglas.Sesar Apr 22 '14 at 19:16
  • Thank you I did and I have expanded it how I wanted it perfectly! Thanks to a wonderful community - I'm sure I have more questions to ask :) – user3502952 Apr 22 '14 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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