Support » Developing with WordPress » How to save metabox data with foreach

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz


    You cannot have multiple input elements with the same name. You end up with only the last value in $_POST[‘my_image_URL’]. Instead, use the name “my_image_URL[]”. This will collect all the field values into an array. OK, that’s still the same name in each field, but the square brackets cause an array to be built, so it’s as though there was a unique index value inside of each bracket, so not really the same name internally.

    Also, don’t loop through the array to save individual values, these are difficult to manage. Just save the entire array as a single meta value. The array is automatically serialized and unserialized, so when you get the meta value, you already have the full array ready to loop through.

    Thanks its worked for me, but how do I edit (update, delete or add) just one or a few value fields?

    How do I specifically access some of the data that is serialized in DB?

    Moderator bcworkz


    You can edit the input fields in the meta box. The image will not change without some javascript manipulation, but when you save the post the current field values are saved and when the page reloads the correct image will display.

    You can manipulate the data by code by accessing individual array elements before saving in post meta. It’s a standard indexed array. Same when it’s retrieved. Data within any element can be altered prior to output.

    You can also directly alter the serialized data through phpMyAdmin, but it’s not recommended because it’s very easy to corrupt the structure. Minor edits are possible if you take care to update the associated string size to match the edit. More elaborate edits would be a bad idea. Either way, backup the table before doing anything, just in case.

    Thanks, your support helped me a lot.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to save metabox data with foreach’ is closed to new replies.