Make WordPress Core

Opened 6 years ago

Last modified 13 days ago

#30854 assigned defect (bug)

`wp_insert_post` doesn't save revision for new published post

Reported by: JamesDiGioia Owned by: adamsilverstein
Milestone: 5.7 Priority: normal
Severity: normal Version:
Component: Revisions Keywords: needs-patch
Focuses: Cc:


Calling wp_insert_post with post_status of publish or any other status on a new post doesn't save a new revision.

The reason for this is the $update check at the beginning:


checks whether or not the args include an ID, indicating that we're updating a post. Further down, that update check is what calls post_updated


which wp_save_post_revision is hooked to. Therefore, if we call wp_insert_post in a plugin to insert a post with apost_status of publish, WordPress doesn't save a post revision.

Post revisions are supposed to be up-to-date with the latest post data, but we don't get a revision if we insert a post in this manner. I think the reason we ended up with this is to avoid saving a revision of the auto-draft that is created when opening up post-new.php, but that isn't necessary now, as wp_save_post_revision checks whether the post_status is an auto-draft.

I think the easiest solution is to move the wp_save_post_revision to the save_post hook, but I'm not sure if there are other places in the codebase this would cause issues.

Attachments (3)

test_wp_first_revision_is_not_lost.diff (973 bytes) - added by georgestephanis 4 years ago.
test_wp_first_revision_is_not_lost.2.diff (1.4 KB) - added by adamsilverstein 4 years ago.
30854.diff (5.9 KB) - added by adamsilverstein 4 years ago.

Download all attachments as: .zip

Change History (15)

#1 @DrewAPicture
6 years ago

  • Component changed from Database to Revisions

#2 @adamsilverstein
6 years ago

  • Keywords needs-patch added
  • Owner set to adamsilverstein
  • Status changed from new to assigned

Hey JamesDiGioia!

Thanks for the bug report. Oh thats a problem, I can see why you aren't getting an initial revision for your posts! We didn't think about posts created this way when creating the original post revision.

I'm not sure moving save_post is good, I think that action fires twice on publish. We could try the change (want to try writing a patch for that?) and run our tests, I'd still worry about unintended consequences. Open to other ideas...

In the mean time, a workaround worth trying would be:

  • use wp_insert_post to create an initial post and get the the post ID
  • use wp_update_post to insert your post data

#3 @JamesDiGioia
6 years ago

The only other thing I can think of is to change the $update variable to be true if it either has an ID or the post_status !== auto-draft. Or if it only == 'publish'. Does that make more sense?

I ended up working around it without an issue in my own plugin, but I think it's still a bug.

#4 @DrewAPicture
6 years ago

  • Version trunk deleted

This ticket was mentioned in Slack in #core-customize by ocean90. View the logs.

4 years ago

#6 @georgestephanis
4 years ago

#39011 was marked as a duplicate.

#7 follow-up: @georgestephanis
4 years ago

Unit test attached, demonstrating the issue.

This affects Core as of 4.7-RC1 with Custom CSS losing its first revision.

Last edited 4 years ago by georgestephanis (previous) (diff)

#8 in reply to: ↑ 7 @adamsilverstein
4 years ago

Replying to georgestephanis:

Unit test attached, demonstrating the issue.

Thanks for the unit test @georgestephanis, thats a great start towards solving this issue.

#9 @adamsilverstein
4 years ago

Still digging into this issue, however I posted test_wp_first_revision_is_not_lost.2.diff to demonstrate this workaround by calling wp_update_post immediately after creating the post, passing the returned post object, the issue is resolved. I think this actually mirrors what happens when you create a post in the admin.

#10 @adamsilverstein
4 years ago

In 30854.diff:

  • Hook wp_save_post_revision on to new_to_publish in addition to update_post - triggering a revision save when a new post is created
  • Adjust revisions unit test to accommodate change: bump expected revision counts by 1, add first revision to order by comparisons

This needs more testing, especially around creating posts in the admin interface and verifying that we aren't getting duplicate initial revisions.

#11 @westonruter
4 years ago

In 39477:

Customize: Ensure a custom_css post insertion gets an initial post revision.

Props georgestephanis, westonruter.
See #30854, #38672, #35395.
Fixes #39032.

#12 @adamsilverstein
13 days ago

  • Milestone set to 5.7
Note: See TracTickets for help on using tickets.