Fix duplicate meta and closed comment status when adding a custom field to an auto-draft - #13349
Fix duplicate meta and closed comment status when adding a custom field to an auto-draft#13349shameemreza wants to merge 1 commit into
Conversation
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
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
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 throughedit_post(). That function readsmetakeyinputandmetavaluefrom$_POST, which the AJAX request body contains, so the meta is added there first. Control then returns towp_ajax_add_meta(), which callsadd_meta()again. The sameedit_post()call passes nocomment_statusorping_status, so_wp_translate_postdata()defaults both toclosed. The block editor only sendscomment_statusover 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_lastmeta is still set as before.Includes unit tests covering both symptoms. They fail against the current code and pass with this change:
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_postmetaand the post'scomment_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.