Make WordPress Core

Opened 6 years ago

Last modified 10 days ago

#47576 reopened defect (bug)

Duplicate Custom Fields are created in new post

Reported by: mhalamine's profile mhalamine Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Editor Keywords: has-patch
Focuses: Cc:

Description

Duplicate custom fields are created when publishing a post or saving it as draft

Steps to reproduce:
Create a New Post
Add a custom field "custom_field_1" with a value "custom_field_value_1"
Add a custom field "custom_field_2" with a value "custom_field_value_2"
Save Draft or Publish

...you will now see the "custom_field_1" field repeated twice (both with a value of "custom_field_value_1")

Another Case:
Create a New Post
Select a custom field from drop down set value to "custom_field_value_3"
save Draft or Publish
... you will now see that the selected field repeated twice with same value

Attachments (1)

Screen Shot 2019-06-20 at 2.23.38 PM.jpg (105.1 KB) - added by mhalamine 6 years ago.
Screenshot of result

Download all attachments as: .zip

Change History (7)

@mhalamine
6 years ago

Screenshot of result

#1 @goaroundagain
6 years ago

I think that's the desired behavior: https://wordpress.org/support/article/custom-fields/

"Keys can be used more than once per post. For example, if you were reading two different books (perhaps a technical book at work and a fiction at home), you could create a “reading” key and use it twice on the same post, once for each book."

If you use the php function add_post_meta you can set $unique = true: https://codex.wordpress.org/Function_Reference/add_post_meta

"Whether or not you want the key to stay unique. When set to true, the custom field will not be added if the given key already exists among custom fields of the specified post."

#2 @mhalamine
6 years ago

You are right about using the key twice but the issue is not here, the issue is that i created only one field but after i saved i got 2 identical entries for the same key.

Try the following:
Create a New Post
Add a custom field "custom_field_1" with a value "custom_field_value_1"
Save Draft or Publish
...you will now see the "custom_field_1" field repeated twice (both with a value of "custom_field_value_1")

#3 follow-up: @garrett-eclipse
6 years ago

  • Component changed from Options, Meta APIs to Editor
  • Keywords needs-patch removed
  • Milestone Awaiting Review deleted
  • Resolution set to reported-upstream
  • Status changed from new to closed
  • Version 5.2.2 deleted

Thanks for the report @mhalamine I was also able to reproduce this but only in the block editor (Gutenberg) and not the classic editor.

I found it's already been reported on the Gutenberg GitHub repo here;
https://github.com/WordPress/gutenberg/issues/11976

As such am closing this as reported-upstream where I made a reference to this ticket. If you have further information please supply it on that ticket as any fix will be worked on there first and then merged into core.

All the best

#4 in reply to: ↑ 3 @mhalamine
6 years ago

Replying to garrett-eclipse:

Thanks for the report @mhalamine I was also able to reproduce this but only in the block editor (Gutenberg) and not the classic editor.

I found it's already been reported on the Gutenberg GitHub repo here;
https://github.com/WordPress/gutenberg/issues/11976

As such am closing this as reported-upstream where I made a reference to this ticket. If you have further information please supply it on that ticket as any fix will be worked on there first and then merged into core.

All the best

@garrett-eclipse thanks for the effort. I will try to debug locally and see if i can come up with extra information that might help.

#5 @topdownjimmy
11 days ago

  • Resolution reported-upstream deleted
  • Status changed from closed to reopened

This is occurring still (or, perhaps, occurring again), as confirmed in this GitHub Gutenberg issue: https://github.com/WordPress/gutenberg/issues/69281#issuecomment-2678491228

This ticket was mentioned in PR #8478 on WordPress/wordpress-develop by @vyasnaman.


10 days ago
#6

  • Keywords has-patch added

This PR solves the issue of duplicate custom fields are added for new post. This comment explains the reason behind this bug.

To fix this issue, I've removed the duplicate call to add_meta from edit_post function. The reason:

  1. Custom fields are handled via AJAX with proper error handling. Hence, we don't need to worry about them being saved.
  2. There's no error handling for add_meta inside edit_post. Hence, if for some reason, add_meta failed to save the custom field then the user won't be aware.

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

Note: See TracTickets for help on using tickets.