0

An administrator wants to make posts for a future date but not visible for visitors. The posts come from a custom WP_Query inside my plugin so the basical WP behaviour does not work in this case.

I tried this :

$args = array(
    'post_type' => "my-post-type",
    'date_query'          => array(
        'before' => 'today',
        'inclusive' => true
    )
);

$args['posts_per_page'] = -1;
...   
return new \WP_Query( $args );

But if I wrote a post with the post_date is today, it does not appear. How can I include the post with post_date before today and today (1 second before now) ?

0

You need to pass a strtotime() compatible string or an array instead of 'today'.

You have several options...

  1. You could change 'today' to 'now'. This will use the current time not midnight last night.
  2. You could change 'today' to 'today + 1 day'. This will be the end of today. Not exactly what you are asking for but worth mentioning.
  3. You could generate it with PHP like date('d-m-Y H:i:s'). This is unnecessary extra work since it will need to be parsed just like 'now'.

Of all of these options, 1 is the simplest and most comprehendable.

Documentation

https://developer.wordpress.org/reference/classes/wp_query/#date-parameters

https://www.php.net/strtotime

0

it looks like user D. Morell already gave you a good answer, but based on your story you can simplify this whole process pretty easily by just making the publish date of those future posts the future date so that they are not available on the site until those dates.

  • Yes, I tried but in my case, I need to hide "private" post and "future" post is only for "public" ones with future date. – J.BizMai Sep 5 at 17:59

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.