1

I would like to give my user the ability to publish a post and the post only becomes visible (like with a published status) when the date hits. I'm using the following code to try and set it to published but on a future date:

$bmt_post = array(
    'post_title'    => wp_strip_all_tags( $title ),
    'post_content'  => $information,
    'post_status'   => 'publish',
    'post_category' => array( $cat_ids ),
    'post_date_gmt' => $postdate,
    'post_date' => $postdate,
);
$post_id = wp_insert_post( $bmt_post );

When I do this, the post is already visible even though it's scheduled for a future date. If I remove the 'post_status' => 'publish' then it creates the post as a draft but then someone would have to go back on the date and change the status from draft to published.

How can I do this so when the user creates the post it's created for the future and only becomes visible when the post_date is the actual date? I know there is a 'post_status' => 'future' but does that automatically convert to published on that date?

2

From digging through /wp-includes/post.php, it looks like you may need to do the following:

  1. set your post_status to future
  2. set the post_date to when you want it published
  3. insert the post as your code shows

Run the following code after inserting the post

$time = strtotime( $postdate . ' GMT' );
wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) );

That is what wordpress uses internally to schedule future posts. check the following function for more information

check_and_publish_future_post( $post_id )

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.