php – Is it possible to update the dataset using update_post_meta

Question

I am trying to create two custom fields for image and text.
I have two inputs in the loop:

...
<input type="hidden" name="wt_set['upload_set_img'][]" id="img" value="100" />
<input type="text" name="wt_set['title'][]" value="Text One" />

<input type="hidden" name="wt_set['upload_set_img'][]" id="img1" value="200" />
<input type="text" name="wt_set['title'][]" value="Text Two" />
...

The result is this $_POST[‘wt_set’]

array(2) {
  ["upload_set_img"]=>
  array(2) {
    [0]=>
    string(3) "100"
    [1]=>
    string(3) "200"
  }
  ["title"]=>
  array(2) {
    [0]=>
    string(3) "Text One"
    [1]=>
    string(3) "Text Two"
  }
}

Then I do this:

foreach( $_POST['wt_set'] as $name => $arr_values ){
    foreach( $arr_values as $i => $value){
        update_post_meta( $post_id, $name, $value );
    }
}

But I only get one value:

  ["upload_set_img"]=>
  array(1) {
    [0]=>
    string(3) "100"
  }
  ["title"]=>
  array(1) {
    [0]=>
    string(4) "Text One"
  }

I need get_post_meta($post->ID,'upload_set_img'); and get_post_meta($post->ID,'title'); equaled:

["upload_set_img"]=>
  array(1) {
    [0]=>
    string(3) "100"
    [1]=>
    string(3) "200"
  }
  ["title"]=>
  array(1) {
    [0]=>
    string(4) "Text One"
    [1]=>
    string(4) "Text Two"
  }

How do I properly use update_post_meta to get this result?

0
VladSiy 5 months 2021-07-10T02:42:14-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse