[wp-trac] [WordPress Trac] #55129: WP REST API - Updating "status" field on a "post" from "future" to "publish" does not work
WordPress Trac
noreply at wordpress.org
Fri Feb 11 00:46:13 UTC 2022
#55129: WP REST API - Updating "status" field on a "post" from "future" to
"publish" does not work
-------------------------------+------------------------------
Reporter: mcmwebsol | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: REST API | Version: 4.7
Severity: normal | Resolution:
Keywords: reporter-feedback | Focuses:
-------------------------------+------------------------------
Changes (by TimothyBlynJacobs):
* keywords: => reporter-feedback
* version: 5.9 => 4.7
Comment:
Welcome to trac, and thanks for the ticket @mcmwebsol!
The reason you are seeing this behavior is that `wp_insert_post`
[https://github.com/WordPress/wordpress-
develop/blob/968b3fc5a4aa9871ce8654b48502f8710fc804a1/src/wp-
includes/post.php#L4236 requires] a publish date that is within a minute
of the current time when publishing a post. Otherwise, the `post_status`
is forced to be `future`.
So when you update the post to `publish` make sure to also pass a new date
that is set to the current time.
The REST API could automatically set the date to now when passing a
`status` of `publish`. That would be inserting magic into the REST API
controller that bypasses the safe guards in `wp_insert_post`, so I'm not
sure we'd want to introduce that behavior. Thoughts @spacedmonkey?
Here is sample code that you can execute with WP-CLI to see that behavior
in action.
{{{#!php
<?php
$request = new WP_REST_Request( 'POST', '/wp/v2/posts' );
$request->set_body_params( [
'status' => 'future',
'date_gmt' => gmdate( 'Y-m-d\TH:i:s', strtotime( 'tomorrow' ) ),
'title' => 'Test Post',
'content' => 'Test Content',
] );
$response = rest_do_request( $request );
if ( is_wp_error( $response ) ) {
wp_die( $response );
}
$request = new WP_REST_Request( 'PUT', '/wp/v2/posts/' .
$response->get_data()['id'] );
$request->set_body_params( [ 'status' => 'publish', 'date_gmt' => gmdate(
'Y-m-d\TH:i:s' ) ] );
$response = rest_do_request( $request );
if ( is_wp_error( $response ) ) {
wp_die( $response );
}
echo $response->get_data()['status'] . PHP_EOL;
echo $response->get_data()['date'] . PHP_EOL;
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/55129#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list