0

I am trying to update meta field value. Everything ok, problem is that field value does not showing in field after refreshing the page.

For example, when I update name value then its does not show in field value after refreshing the page.

<?php  
     update_post_meta($post_id, 'auction_model', trim( $_POST['Product_model']));
     $year=get_post_meta($post_id , 'Product_Year',true );
<form id="wdm-add-auction-form" class="auction_settings_section_style" action="" method="POST">
<table>
<tr valign="top">
    <th scope="row">
        <label for="auction_model"><?php _e('Product Model', 'wdm-ultimate-auction');?></label>
    </th>
    <td>
        <input name="auction_model" type="text" id="auction_model" class="regular-text" value="<?php echo $model;?>"/>
    </td>
</tr>
</table>

</form>
?>

1 Answer 1

0

You are not retrieving the saved value for $model.

$model = get_post_meta( $post_id, 'auction_model', true );

And you should be sanitizing the values upon retrieval, or at least before echoing them:

<?php echo sanitize_text_field( $model ); ?>


You could also check for the value before calling update_post_meta() upon form submission, unless you intended to run that every time this page is loaded. If nothing else, you can at least check if $_POST has a value:

if ( isset( $_POST['auction_model'] && ! empty( $_POST['auction_model'] ) ) {
    update_post_meta( $post_id, 'auction_model', sanitize_text_field( $_POST['auction_model'] ) );
}

You can also then delete meta if the form field is empty:

else { 
     delete_post_meta( $post_id, 'auction_model' );
}

You can read more on sanitizing and validating user input here.

And more on the three *_post_meta() functions mentioned above here:

update_post_meta()

delete_post_meta()

get_post_meta()

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.