update post meta wordpress

ok your problem is that each loop is updating every item. This is because you are testing if there is a value for $_POST, up date it. You need to set a identifier so it knows which post to update.

for example, but the post id in your input field.

<input type='text' name='doors[<?php echo $auto->ID; ?>]' value='<?php echo isset($doors) ? $doors : ''; ?>' />

then you can update from this variable.

if ( isset( $_POST['doors'] ) ){  
// verify nounce prob a good idea 
    foreach($_POST['doors'] as $item=>$key) {
        $id= sanitize_text_field($item);
        update_post_meta( $id, 'doors', sanitize_text_field( $key ) );

note this $_POST code appears in the loop (in-between the foreach statement and endforeach). move this to outside the loop at the top of the page to save it running everytime. Now also you can update multiple values with each submission. You will also need to check the values somehow to prevent someone changing the postmeta on any post they fancy (all they need to do it post[doors][postid] to change any doors value).

Leave a Comment