WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#40012 new defect (bug)

Only add_metadata if no matching value

Reported by: johnjamesjacoby Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 2.9
Component: Options, Meta APIs Keywords: 2nd-opinion
Focuses: Cc:
PR Number:

Description

add_metadata() has a $unique parameter, but it only looks for a unique meta_key for the given object ID. I have a need to add meta-data with the same meta_key but different values, and only if that meta_value isn't already present with the same key.

To do this now, requires:

  • Getting all meta-data for the ID by meta_key
  • Checking those array-values for my value
  • Bailing if it's already present, or...
  • Proceeding with $unique set to false

It'd be lovely if add_metadata() either:

  • Accepted strings like key, value for the $unique parameter, to switch up the check
  • Added a second parameter for unique value in addition to a unique key

Attachments (1)

40012.diff (2.2 KB) - added by johnjamesjacoby 3 years ago.
Proof of concept. Needs testing with serialized data.

Download all attachments as: .zip

Change History (2)

@johnjamesjacoby
3 years ago

Proof of concept. Needs testing with serialized data.

#1 @johnjamesjacoby
3 years ago

Thinking about this more, there could be 4 types of uniqueness:

  • Unique meta_key to all objects
  • Unique meta_key to the $object_id (what $unique currently checks)
  • Unique meta_key and meta_value to all objects
  • Unique meta_key and meta_value the $object_id (what my patch adds)
Note: See TracTickets for help on using tickets.