0

I have an array of values to store in the post meta, my post data looks like below JSON representation

[
  {
    "field_1": "value 1",
  },

  {
    "field_1": "value 2",
  },

  {
    "field_1": "value 3",
  },


]

And each item can have multiple keys like field_2, field_3 other than field_1, i have converted the JSON to array, and looped through each item and store in meta data by calling add_post_meta() function, but the problem arises when i want to have a duplicate in this list like this

    [
      {
        "field_1": "value 1",
      },
      {
        "field_1": "value 1",
      },
   ]

since i need the ability to update each item, i need to find a way to make the meta to properly update duplicate values, because update_post_meta take previous value and current value so if i update field_1 to any value, the second item might be updated to same value of first item, how to handle this problem?

2
  • Looking through the code of update_metadata() this seems not easily possible. Have you considered writing a custom SQL query instead? Also, it might sound like taxonomies could fit you better than postmeta, but from this little information it is impossible to tell. Maybe you can elaborate on what you're trying to achieve in the question? – kero Feb 5 '20 at 15:10
  • @kero, i need to store a list in the post meta, but i want the each item to be updatable, deletable, i am trying to acheive that. The problem with current implementation is if i have items with duplicate data, if i change data in one of the item with update, it is going to update the second one in the list – Naveen Feb 5 '20 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.