diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 98b9092f16f1f..bf13ea6ce477a 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -5129,8 +5129,11 @@ function wp_update_post( $postarr = array(), $wp_error = false, $fire_after_hook } // Drafts shouldn't be assigned a date unless explicitly done so by the user. + // Additionally, if the post_status is being updated to 'publish', 'future', or 'private', + // do not clear the date, as the provided date should take precedence. if ( isset( $post['post_status'] ) && in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) + && ( ! isset( $postarr['post_status'] ) || ! in_array( $postarr['post_status'], array( 'publish', 'future', 'private' ), true ) ) && empty( $postarr['edit_date'] ) && ( '0000-00-00 00:00:00' === $post['post_date_gmt'] ) ) { $clear_date = true; diff --git a/tests/phpunit/tests/post/wpUpdatePostDateStatus.php b/tests/phpunit/tests/post/wpUpdatePostDateStatus.php new file mode 100644 index 0000000000000..1a052d2bfe58c --- /dev/null +++ b/tests/phpunit/tests/post/wpUpdatePostDateStatus.php @@ -0,0 +1,68 @@ + array( + 'initial_status' => 'pending', + ), + 'draft to future' => array( + 'initial_status' => 'draft', + ), + ); + } + + /** + * Test that wp_update_post() preserves post_date when changing to future status + * + * @ticket 62468 + * + * @dataProvider data_post_status_transitions + * @covers ::wp_update_post + * + * @param string $initial_status Initial post status + */ + public function test_update_post_preserves_date( $initial_status ) { + $post_data = array( + 'post_title' => 'Test Post', + 'post_content' => 'Test content', + 'post_status' => $initial_status, + 'post_author' => self::factory()->user->create( array( 'role' => 'editor' ) ), + ); + + $post_id = wp_insert_post( $post_data ); + $this->assertIsInt( $post_id ); + + // Set future date (1 day from now) + $future_date = gmdate( 'Y-m-d H:i:s', strtotime( '+1 day' ) ); + $future_date_gmt = get_gmt_from_date( $future_date ); + + $update_data = array( + 'ID' => $post_id, + 'post_status' => 'future', + 'post_date' => $future_date, + 'post_date_gmt' => $future_date_gmt, + ); + + $update_result = wp_update_post( $update_data ); + $this->assertIsInt( $update_result ); + + $updated_post = get_post( $post_id ); + + $this->assertEquals( 'future', $updated_post->post_status ); + $this->assertEquals( $future_date, $updated_post->post_date ); + $this->assertEquals( $future_date_gmt, $updated_post->post_date_gmt ); + } +}