Make WordPress Core

Opened 8 weeks ago

Last modified 8 weeks ago

#62468 new defect (bug)

wp_update_post overwrites post_date when updating post_status

Reported by: aguerra07's profile aguerra07 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch needs-testing needs-unit-tests reporter-feedback
Focuses: Cc:

Description

Steps to reproduce the bug:

  1. Create a post with the status pending.
  2. Use wp_update_post to update the post status to future and set post_date to a specific future date.
  3. Observe that the post_date value is reset, and the post is immediately published.

Expected behavior:
When updating the post status to future and setting a future post_date, the post should be scheduled for publication at the specified date and not published immediately.

Actual behavior:
The post_date is recalculated based on the current date and time due to the existing post status, causing the post to be published immediately.

Cause:
The wp_update_post function uses the current post status to determine whether the post_date should be modified. This logic does not account for updates to the post status in the same operation.

Suggested fix:
Adjust the logic in wp_update_post to prioritize updates to the post status before checking conditions that modify the post_date. Adding the edit_date flag to enforce the use of the provided post_date is a temporary workaround, but a deeper fix is needed to handle this logic cleanly.

Patch:
A proposed patch file is attached, which modifies the wp_update_post function to handle this scenario correctly.

Attachments (5)

fix-wp-update-post.patch (889 bytes) - added by aguerra07 8 weeks ago.
This patch fixes a bug in wp_update_post that could clear the post_date when updating the post status, ensuring it is preserved when explicitly set.
before-patch-1.png (101.7 KB) - added by parthvataliya 8 weeks ago.
before-patch-2.png (99.1 KB) - added by parthvataliya 8 weeks ago.
after-patch-1.png (107.7 KB) - added by parthvataliya 8 weeks ago.
after-patch-2.png (110.7 KB) - added by parthvataliya 8 weeks ago.

Download all attachments as: .zip

Change History (10)

@aguerra07
8 weeks ago

This patch fixes a bug in wp_update_post that could clear the post_date when updating the post status, ensuring it is preserved when explicitly set.

#1 @ankitkumarshah
8 weeks ago

Hi @aguerra07,

Thank you for bringing this up. I attempted to reproduce the issue, but I was unable to replicate it. Below are the details of my implementation:

My create_pending_post() Function:

function create_pending_post() {
	$post_data = array(
		'post_title'   => 'Your Post Title',
		'post_content' => 'Your post content goes here.',
		'post_status'  => 'pending',
		'post_author'  => 1,  
		'post_type'    => 'post',
	);

	$post_id = wp_insert_post( $post_data );

	if ( ! is_wp_error( $post_id ) ) {
		return 'Post created successfully with ID: ' . $post_id;
	} else {
		return 'Error creating post: ' . $post_id->get_error_message();
	}
}

My schedule_post_for_future() Function

function schedule_post_for_future( $post_id ) {
	$post_id     = 62;
	$future_date = '2024-12-25 14:30:00';
	if ( ! strtotime( $future_date ) ) {
		return 'Invalid date format';
	}

	$post_data = array(
		'ID'            => $post_id,
		'post_status'   => 'future',
		'post_date'     => $future_date,
		'post_date_gmt' => get_gmt_from_date( $future_date ),
	);

	$updated_post = wp_update_post( $post_data, true );

	if ( is_wp_error( $updated_post ) ) {
		return 'Error updating post: ' . $updated_post->get_error_message();
	}

	return 'Post successfully scheduled for: ' . $future_date;
}

My Post Dashboard:

https://i.postimg.cc/WVHNSk2v/image.png

Database Info:

https://i.postimg.cc/mDxpvNwb/image.png

Could you please let me know if there’s something I might have overlooked or if there are additional steps I should try to reproduce the issue?

WordPress Version: 6.7

Last edited 8 weeks ago by ankitkumarshah (previous) (diff)

#2 @parthvataliya
8 weeks ago

Test Report

Description

This report validates whether the indicated patch works as expected.

Patch tested: https://core.trac.wordpress.org/attachment/ticket/62468/fix-wp-update-post.patch

Environment

  • WordPress: 6.8-alpha-59274-src
  • PHP: 8.2.22
  • Server: nginx/1.27.0
  • Database: mysqli (Server: 8.0.39 / Client: mysqlnd 8.2.22)
  • Browser: Chrome 129.0.0.0
  • OS: Linux
  • Theme: Twenty Twenty-Five 1.0
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.2.0

Actual Results

  1. ✅ Issue resolved with patch.

This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.


8 weeks ago

#4 @desrosj
8 weeks ago

  • Focuses performance coding-standards php-compatibility removed
  • Keywords needs-unit-tests reporter-feedback added

Hi @aguerra07,

Welcome to Trac!

This one will definitely require unit tests that first demonstrate the initial problem, and then demonstrate the changes work as expected with out side effects.

Creating a pull request on GitHub will run the entire test suite against your suggested code changes to confirm there are no unexpected consequences.

Also, can you proved more context to the problem? Did this just start happening after updating to 6.7? Or was has this always been the behavior?

#5 @mukesh27
8 weeks ago

  • Version 6.7 deleted

Removing 6.7 as it's not latest version issue.

Note: See TracTickets for help on using tickets.