Skip to content

Fix duplicate meta and closed comment status when adding a custom field to an auto-draft - #13349

Open
shameemreza wants to merge 1 commit into
WordPress:trunkfrom
shameemreza:fix/66016-add-meta-auto-draft
Open

Fix duplicate meta and closed comment status when adding a custom field to an auto-draft#13349
shameemreza wants to merge 1 commit into
WordPress:trunkfrom
shameemreza:fix/66016-add-meta-auto-draft

Conversation

@shameemreza

Copy link
Copy Markdown

Adding a custom field to a new post (auto-draft) through the legacy Custom Fields meta box inserts the meta twice and switches the post's comment and ping status to closed.

wp_ajax_add_meta() promotes the auto-draft to a draft through edit_post(). That function reads metakeyinput and metavalue from $_POST, which the AJAX request body contains, so the meta is added there first. Control then returns to wp_ajax_add_meta(), which calls add_meta() again. The same edit_post() call passes no comment_status or ping_status, so _wp_translate_postdata() defaults both to closed. The block editor only sends comment_status over the REST API when the Discussion panel was changed, so the closed status sticks after publishing.

This change promotes the auto-draft with wp_update_post() instead. The meta is inserted exactly once, the post keeps its comment and ping status, and the _edit_last meta is still set as before.

Includes unit tests covering both symptoms. They fail against the current code and pass with this change:

1) test_adding_meta_to_an_auto_draft_should_not_duplicate_the_meta
Failed asserting that two arrays are identical.
 Array &0 (
     0 => 'testvalue'
+    1 => 'testvalue'
 )

2) test_adding_meta_to_an_auto_draft_should_not_change_the_comment_and_ping_status
The comment status of the post should not have changed.
-'open'
+'closed'

To reproduce manually: on a clean install with the Custom Fields panel enabled, open Posts > Add New, add a custom field before saving, then check wp_postmeta and the post's comment_status.

Trac ticket: https://core.trac.wordpress.org/ticket/66016

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Used for: diagnosis, draft patch, and test suggestions. I reviewed, tested, and edited the final change.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Adding a custom field to an auto-draft via the Custom Fields meta box
runs wp_ajax_add_meta(), which promoted the post through edit_post().
That call added the meta a first time from the $_POST data before
wp_ajax_add_meta() added it again, and _wp_translate_postdata()
defaulted the missing comment and ping status to 'closed'. The block
editor never resends comment_status over the REST API, so the closed
status stuck after publishing.

Promote the auto-draft with wp_update_post() instead. The meta is added
exactly once and the post keeps its comment and ping status.

See https://core.trac.wordpress.org/ticket/66016
@shameemreza
shameemreza marked this pull request as ready for review September 1, 2026 11:45
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props shameemreza.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant