Browse by section

Web Design & Dev 日本語

When Does a WordPress REST API Update Publish a Post?

This blog uses the WordPress REST API to automate publishing. For a while, I believed that updating a draft whose date had already passed would publish it automatically, so my tool reset the draft’s date to the next day on every update.

The short answer: in my WordPress 7.1 tests, a draft was never published by a REST API update. A post went live on update only when it was a scheduled post whose date had passed. Separately, publishing a draft with a past date makes it go live immediately instead of scheduling it.

This article shows the results for each combination of status and date, the WordPress core code behind them, and the checks I now run.

Sponsored

When does a WordPress REST API update publish a post?

In two cases: when you update a scheduled post (status=future) whose date has passed, and when you send status=publish to a draft with a past date.

On a local WordPress 7.1 site, I created posts, updated a single field through the REST API, and read the status back. The site timezone was Asia/Tokyo.

Post before update What I sent Status after update
Draft dated 3 days ago featured_media only Still a draft
Draft dated 3 days ago title only Still a draft
Draft dated 3 days ahead featured_media only Still a draft
Draft dated 3 days ago Custom field only, via XML-RPC Still a draft
Scheduled (future) post past its date featured_media only Published (publish)
Draft dated 3 days ago status=publish Published, date still 3 days ago
Draft dated 3 days ahead status=publish Scheduled (future)
Draft dated 3 days ago status=draft plus tomorrow’s date Still a draft (only the date changed)

This blog uses Polylang for its Japanese and English versions. With Polylang 3.8.9 active, I repeated the “draft dated 3 days ago, featured image only” case. It also stayed a draft.

Why does updating an overdue scheduled post publish it?

Because wp_insert_post(), which saves every post, switches a scheduled post whose date has passed to published on each save.

A REST API update goes through wp_update_post() and then wp_insert_post(). In WordPress 7.1, wp-includes/post.php contains this block:

if ( 'attachment' !== $post_type ) {
	$now = gmdate( 'Y-m-d H:i:s' );

	if ( 'publish' === $post_status ) {
		if ( strtotime( $post_date_gmt ) - strtotime( $now ) >= MINUTE_IN_SECONDS ) {
			$post_status = 'future';
		}
	} elseif ( 'future' === $post_status ) {
		if ( strtotime( $post_date_gmt ) - strtotime( $now ) < MINUTE_IN_SECONDS ) {
			$post_status = 'publish';
		}
	}
}

Only publish and future are switched. A draft never enters either branch, which matches the drafts in the table staying drafts.

When does a scheduled post stay “future” after its date?

When WP-Cron, which publishes scheduled posts, does not run at the scheduled time.

Normally, WP-Cron’s publish_future_post event publishes a scheduled post at its time. WP-Cron runs when someone visits the site, so a post can stay future after its time if there is no traffic, or if DISABLE_WP_CRON is set.

In my test, I enabled DISABLE_WP_CRON and scheduled a post 70 seconds ahead. After 80 seconds it was still future. Sending only a featured image then turned it into publish. “I only changed the featured image and the post went live” is reproducible under exactly this condition.

Sponsored

What happens when you publish a draft with a past date?

It is published immediately, not scheduled, and it keeps the past date.

The code above only turns publish into future when the date is at least one minute in the future. With a past date, the post is saved as publish.

My posting tool sets a draft’s date to 19:00 the next day. The idea is that clicking “Publish” in the admin schedules the post for that time. If a draft sits for a few days before you click “Publish”, it goes live immediately with a date in the past, so it lands further down the newest-first list on the home page.

What did I change after testing this?

Resetting a draft’s date on every update did nothing to prevent automatic publishing. What I should have checked was whether the post was a future post past its date.

The original safeguard came from an incident where a post went live after its featured image was replaced. I have no record of whether that post was a draft or a scheduled post, so I cannot check it now. I blamed “a draft with a past date” at the time, but these results show that understanding was wrong.

As explained in Blog Policy and How I Think About Writing Articles, I correct mistakes openly instead of quietly editing them away. That is why the original misunderstanding stays in this article.

▼Checks before and after updating a post via the REST API

① Read status and date before updating. If status=future and the date has passed, the update will publish it

② Before publishing a draft, confirm its date is in the future. A past date means immediate publication

③ Read the post again after updating, and confirm status and date did not change unexpectedly

In Python, it looks like this. It compares the post before and after the update and stops if anything changed.

from datetime import datetime
from zoneinfo import ZoneInfo
import requests

def get_post(post_id):
    r = requests.get(f"{API}/posts/{post_id}", params={"context": "edit"}, headers=AUTH, timeout=30)
    r.raise_for_status()
    return r.json()

before = get_post(post_id)
scheduled = datetime.fromisoformat(before["date"]).replace(tzinfo=ZoneInfo("Asia/Tokyo"))
if before["status"] == "future" and scheduled <= datetime.now(ZoneInfo("Asia/Tokyo")):
    raise SystemExit("The scheduled date has passed. Updating will publish this post")

requests.post(f"{API}/posts/{post_id}", json={"featured_media": media_id}, headers=AUTH, timeout=60).raise_for_status()

after = get_post(post_id)
for key in ("status", "date"):
    if before[key] != after[key]:
        raise SystemExit(f"{key} changed: {before[key]} -> {after[key]}")

The date field is returned in the site’s timezone, so I attach the site timezone (Asia/Tokyo for this blog) before comparing.

Sponsored

How can you run the same test on a local WordPress?

WordPress Playground CLI starts a local WordPress with one command, without installing PHP or Docker.

Testing on a production site would publish the test posts for real. Instead, I started WordPress 7.1 with WordPress Playground CLI, which runs on Node.js, and sent REST API requests to it.

npm i @wp-playground/cli
npx wp-playground-cli server --blueprint=bp.json --port 9400

The bp.json blueprint disables WP-Cron and creates an application password for REST API authentication.

{
  "preferredVersions": { "php": "8.3", "wp": "latest" },
  "steps": [
    { "step": "defineWpConfigConsts", "consts": { "WP_ENVIRONMENT_TYPE": "local", "DISABLE_WP_CRON": true } },
    { "step": "setSiteOptions", "options": { "timezone_string": "Asia/Tokyo" } },
    { "step": "runPHP", "code": "<?php require '/wordpress/wp-load.php'; $u = get_user_by('login', 'admin'); list($pw) = WP_Application_Passwords::create_new_application_password($u->ID, array('name' => 'rest-test')); file_put_contents('/wordpress/app-pass.txt', $pw); ?>" }
  ]
}

WP_ENVIRONMENT_TYPE is set to local because application passwords only work on HTTPS sites or local environments. After startup, read the password from http://127.0.0.1:9400/app-pass.txt and call the REST API with Basic authentication as the admin user.

I also wrote about turning API checks into scripts in the API testing section of 5 Ways to Make QA Work Faster with Python.

Summary

▼Key points

① In WordPress 7.1, a draft is not published by a REST API update

② A scheduled (future) post past its date is published by any update

③ Publishing a draft with a past date makes it go live immediately instead of scheduling it

④ The switch happens in wp_insert_post(), which swaps publish and future based on the date

⑤ Comparing status and date before and after an update catches unintended publishing

The “it went live when I only updated it” moment was caused by scheduled posts, not drafts. I should have reproduced the problem locally before building a safeguard around it.

Tested with WordPress 7.1 (PHP 8.3, WordPress Playground CLI) on September 12, 2026. Results may differ if a theme or plugin changes how posts are saved.