My brain has fallen over. How do I save this post data? Building some key-value pairs in the form.

    for ($i = 1; $i <= 3; $i++) {
        $saved = $meta[$i];
        //echo $saved['key'];
        echo '<p>';
        echo '<label for="meta_k_'.$i.'">Key</label> ',   '<input id="meta_k_'.$i.'" type="text" name="_meta['.$i.'][key]" value="'.$saved['key'].'" />&nbsp;&nbsp;';
        echo '<label for="meta_v_'.$i.'">Value</label> ', '<input id="meta_v_'.$i.'" type="text" name="_meta['.$i.'][value]" value="'.$saved['value'].'" />';
        echo '</p>';

How do I save them???

    foreach ($_POST['_meta'] as $key => $value) {
        update_post_meta($post_id, $key, $value);

2 Answers 2


This should be work:

foreach ($_POST['_meta'] as $value) {
    update_post_meta($post_id, $value['key'], $value['value']);

you are almost there. just hook your saving logic to 'save_post'.

add_action('save_post', 'custommeta');
function custommeta($post_id)
  //put your update code here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.