WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#15049 closed enhancement (duplicate)

postavoid add_metadata to insert duplicated entries

Reported by: leogermani Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.1
Component: Database Keywords: metadata, post_meta, user_meta
Focuses: Cc:

Description

when calling add_metadata, check if there is already an entry with the same object id, meta_key, and meta_value. If there is, return false so we dont have duplicated entries.

The unique paramater of the add_metadata does not handle with that, since it only cares about the meta_key not beeing duplicated.

When playing with multiple metadatas with the same meta_key, very often you have to check if a value already exists before adding it. I beleive add_metadata should handle it automatically.

Attachments (1)

meta.php.diff (596 bytes) - added by leogermani 4 years ago.
patch for add_metadata function

Download all attachments as: .zip

Change History (6)

leogermani4 years ago

patch for add_metadata function

comment:1 Denis-de-Bernardy4 years ago

When used as so you're expecting WP to store array values as separate meta entries. If you store, say, the tag ids a post had across revisions, each serialized as a meta value, and then add and remove a tag, you'd expect a dup entry in there.

Whether WP should discard duplicate meta key/value pairs should be left to plugin authors imo.

comment:2 follow-up: leogermani4 years ago

In your example, the value would have something to relate with the post revision id, wouldn't it? then it would be different...

What could be a problem though is that there may be some plugins/themes that rely on this "feature" and will break

comment:3 in reply to: ↑ 2 Denis-de-Bernardy4 years ago

Replying to leogermani:

My intent was merely to highlight that there can be a use for arrays that hold the same value multiple times. I've never used it myself but there might be someone out there who does if it works as expected.

If storing identical key/value pairs currently works as expected, I'd recommend wontfix. (It can't be that hard to call in_array() before adding a meta.) If it doesn't, might as well do make WP do the check indeed.

comment:4 Denis-de-Bernardy4 years ago

Also, I'd change the patch so it fetches the meta using the API and use in_array(). The typical use case for add_meta is to insert an additional value in an array. There are good chances it's in the cache when it gets called.

comment:5 scribu4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.