I'm trying to allow users to add meta values in my posts but it doesn't seem to be adding it in my front end post edit

<!-- edit Post Form -->
<div id="postbox">
<form id="edit_post" name="edit_post" method="post" action="">
</p>
<p><label for="description">Description</label><br />
<textarea id="description" name="description" ><?php echo $post_to_edit->post_content; ?></textarea>
</p>
<!-- wine Rating -->
<fieldset class="data">
          <label for="data">data</label>
          <input type="text" value="" id="data" size="60" tabindex="20" name="data"></textarea>
</fieldset>

<p align="right"><input type="submit" value="Edit" tabindex="6" id="submit" name="submit" /></p>
<input type="hidden" name="action" value="edit_post" />
<input type="hidden" name="pid" value="<?php echo $post_to_edit->ID; ?>" />
<?php wp_nonce_field( 'edit-post' ); ?>
</form>
</div>
<?php

if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "edit_post" && isset($_POST['pid'])) {
$the_post = get_post($_POST['pid']); 


$the_post = array(); 
$the_post['post_content'] = $_POST['description'];
$the_post['data'] = array($_POST['data']);

add_post_meta($pid, 'rating', $the_post['data'], true);

  $pid = wp_update_post($the_post); 


  $link = get_permalink( $pid );
  wp_redirect($link);
}
?>

but I can't seem to get it to work for the front end editing I'm pretty sure I'm doing it wrong help...

link|improve this question
feedback

1 Answer

This code should be conditional first and then form output since you can't use wp_redirect after headers are set and you are updating so use update_post_meta instead of add_post_meta.

Try:

    if( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&  $_POST['action'] == "edit_post" && isset($_POST['pid'])) {
    $the_post = get_post($_POST['pid']); 


    $the_post = array(); 
    $the_post['post_content'] = $_POST['description'];
    $the_post['data'] = array($_POST['data']);

    $pid = wp_update_post($the_post); 
    update_post_meta($pid, 'rating', $the_post['data'], true);

    $link = get_permalink( $pid );
    wp_redirect($link);
}

<!-- edit Post Form -->
    <div id="postbox">
        <form id="edit_post" name="edit_post" method="post" action="">
        <p><label for="description">Description</label><br />
            <textarea id="description" name="description" ><?php echo $post_to_edit->post_content; ?></textarea>
        </p>
        <!-- wine Rating -->
        <fieldset class="data">
                  <label for="data">data</label>
                  <input type="text" value="" id="data" size="60" tabindex="20" name="data"><?php echo (!empty($val = get_post_meta($post_to_edit->ID,'rating',true))) ? $val : ''; ?></textarea>
        </fieldset>

        <p align="right"><input type="submit" value="Edit" tabindex="6" id="submit" name="submit" /></p>
        <input type="hidden" name="action" value="edit_post" />
        <input type="hidden" name="pid" value="<?php echo $post_to_edit->ID; ?>" />
        <?php wp_nonce_field( 'edit-post' ); ?>
        </form>
    </div>
link|improve this answer
no I'm trying to add new meta not update so i cna continually have more metas and i found away to bypass the the headers to use redirect – user7715 Aug 10 '11 at 14:19
update_post_meta does the same thing that add_post_meta so this should still work, try it and report back. – Bainternet Aug 10 '11 at 14:49
doesn't seem to work not really sure how post meta work for edit, doesn't seem to add or change the meta value of the existing meta value – user7715 Aug 10 '11 at 20:24
Well I'm sure how post meta works and if you have a post id and meta value you can use update_post_meta, also i just test this code and it works fine. – Bainternet Aug 10 '11 at 20:31
it works but its not updating the meta value or adding new ones so maybe its just something wrong with the theme – user7715 Aug 10 '11 at 22:40
feedback

Your Answer

 
or
required, but never shown

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