WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
share|improve this question
up vote 1 down vote accepted

This should be work:

foreach ($_POST['_meta'] as $value) {
    update_post_meta($post_id, $value['key'], $value['value']);
share|improve this answer
Thanks! Just couldn't see it :-) – Niels Jun 29 '12 at 8:16

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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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