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.

     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">
<tr valign="top">
    <th scope="row">
        <label for="auction_model"><?php _e('Product Model', 'wdm-ultimate-auction');?></label>
        <input name="auction_model" type="text" id="auction_model" class="regular-text" value="<?php echo $model;?>"/>


1 Answer 1


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:




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.