4

I am running this query to print out my posts. It works but I want to add a parameter that tells the system to only display posts that are from today or will be published in the future!

Here is the query:

$today = getdate();
$year=$today["year"];
$month=$today["mon"];
$day=$today["mday"];

query_posts( $query_string.'order=ASC' .
             '&post.status=future,publish' .
             '&year='.$year .
             '&monthnum='.$month
);

I tried to do something like &post.date = <= $today but that didn't work.

please, can anyone tell me how to do it?

My idea is to tell the query to only show posts with a publishing-date that is today or "less" than today. That's why the " <= " .

pb2q
  • 58,613
  • 19
  • 146
  • 147
user2191254
  • 55
  • 2
  • 6
  • Don't know if you've seen this one -http://stackoverflow.com/questions/9317244/wordpress-only-show-future-posts-minus-one-day – McNab Mar 24 '13 at 16:11
  • I didn't realize WordPress can display posts from the future. All these years, I've been doing it wrong. :-) – Adam Liss Mar 25 '13 at 00:56

3 Answers3

2
$future_args = array(
    'post_status' => 'future'
    // possibly further query arguments
);

$today = getdate();
$today_args = array(
    'year' => $today['year'],
    'monthnum' => $today['mon'],
    'day' => $today['mday'] 
    // possibly further query arguments
);

$future_query = new WP_Query( $future_args );
$today_query = new WP_Query( $today_args );

while ( $today_query->have_posts() ) :
    $today_query->the_post();
    // echo something
endwhile;
wp_reset_postdata();

while ( $future_query->have_posts() ) :
    $future_query->the_post();
    // echo something
endwhile;
wp_reset_postdata();

That ought to do it.

See the codex article on the WP_Query class for reference.

Johannes Pille
  • 4,073
  • 4
  • 26
  • 27
  • Obviously it would be slicker if it were doable in a single query, but I doubt (have not tried) that it is, since future post do not have a date associated with them. Or do they? Anyhow, this will work, albeit maybe not the most efficient. – Johannes Pille Mar 25 '13 at 00:56
  • Hi, thanks! I will try this out in a minute. As far as I know future posts do have a date associated with them. As today is the 25th, I could plan a post to be released on the 30th. The date is then 30-03-2013 and the post_status = future. – user2191254 Mar 25 '13 at 20:35
  • Johannes, thanks a lot! It works! Had to edit my index.php a bit to make it work but now it does... Very cool! Can you help me a tiny bit more? :) Now I want to add these two queries with the exact same function to my archive.php which is used for the category pages. Of course there should be only posts of the certain category. Do you have an idea how I have to modify the code to achieve this on the archive.php? When I just pasted it in there it gave me all the posts from all categories, like on the index. not just the posts from the correct category. THANKS! – user2191254 Mar 25 '13 at 20:54
  • I helped myself with this line: 'cat' => $category_id, – user2191254 Mar 25 '13 at 21:36
  • 1
    so I think I don't need your help anymore.. thanks again for the code! – user2191254 Mar 25 '13 at 21:36
  • You cannot use `2day` or `2day_query` as variables, PHP variables cannot start with a number: http://stackoverflow.com/questions/15427476/why-variable-can-not-start-with-a-number – Rounds Jun 09 '16 at 11:00
  • Wrote this blind (obvously). Apart from this blunder, the above will work as expected though. Instead of commenting and (!) downvoting, why not just edit the answer? – Johannes Pille Jun 10 '16 at 11:10
2

Have you all found that the future posts are only viewable to admins or authors? I wanted to list all future post titles to non members and the query would always return 0 posts when i queried for 'future', unless i was logged in as an admin.

Doing a var dump of the returned array of 0 results i found various settings that were un set and one suppress_filters caught my eye so setting it to 1 (true) i got the result i wanted.

So anyone having this problem i found the solution, its just 1 extra setting in the query 'suppress_filters' => '1'

so for example

$args = array (
    'post_status'           => 'future',
    'suppress_filters'      => '1'
    );
$query = new WP_Query($args);

After searching via google for hours i could not find this simple solution. But there it is hope it helps someone else.

Chris
  • 21
  • 1
1

This approach worked for me, using date_query parameters (available since WP 3.7).

$args = array(
    'post_status' => 'publish,future',
    'date_query' => array(
        array(
            'after' => date('Y-m-d'),
            'inclusive' => true,
        )
    ),
);

$query = new WP_Query($args);

It queries all posts from the whole current day (before or after the current time) and into the future.