0
0
Wordpressframework~5 mins

Post scheduling and status in Wordpress

Choose your learning style9 modes available
Introduction

Post scheduling lets you set a future time for your content to appear automatically. Post status shows if a post is draft, published, or pending.

You want to write a blog post now but publish it tomorrow morning.
You need to review a post before it goes live by setting it as pending.
You want to save a post as a draft to finish later without publishing.
You want to automatically publish posts at specific times without manual action.
Syntax
Wordpress
<?php
// Schedule a post to publish later
$post = array(
  'ID' => 123, // existing post ID
  'post_date' => '2024-07-01 10:00:00', // future date and time
  'post_status' => 'future' // status for scheduled posts
);
wp_update_post($post);

// Change post status
wp_update_post(array(
  'ID' => 123,
  'post_status' => 'draft' // or 'publish', 'pending', 'future'
));
?>

Use 'future' status to schedule posts for future publishing.

Post statuses include 'draft', 'publish', 'pending', and 'future'.

Examples
This sets the post with ID 123 to publish automatically at the given date and time.
Wordpress
<?php
// Schedule a post to publish on July 1, 2024 at 10 AM
wp_update_post(array(
  'ID' => 123,
  'post_date' => '2024-07-01 10:00:00',
  'post_status' => 'future'
));
?>
This changes the post status to draft so it is not visible publicly.
Wordpress
<?php
// Save a post as draft
wp_update_post(array(
  'ID' => 123,
  'post_status' => 'draft'
));
?>
This makes the post live on the site right away.
Wordpress
<?php
// Publish a post immediately
wp_update_post(array(
  'ID' => 123,
  'post_status' => 'publish'
));
?>
Sample Program

This code creates a new post scheduled for one day from now. It then prints the post ID and its status, which will be 'future'.

Wordpress
<?php
// Example: Schedule a new post and check its status
$post_data = array(
  'post_title' => 'My Scheduled Post',
  'post_content' => 'This post will appear later.',
  'post_status' => 'future',
  'post_date' => date('Y-m-d H:i:s', strtotime('+1 day')),
  'post_author' => 1
);

// Insert the post
$post_id = wp_insert_post($post_data);

// Get the post status
$status = get_post_status($post_id);

echo "Post ID: $post_id\n";
echo "Status: $status\n";
?>
OutputSuccess
Important Notes

WordPress uses the 'future' status to mark posts scheduled for later publishing.

Always use the correct date format 'Y-m-d H:i:s' for scheduling.

Scheduled posts will automatically publish at the set time without manual action.

Summary

Post scheduling sets a future publish time using 'post_date' and 'future' status.

Post status controls visibility: 'draft' for unfinished, 'publish' for live, 'pending' for review, and 'future' for scheduled.

Use wp_insert_post or wp_update_post to set scheduling and status in WordPress.