Updating Metadata in WordPress

In previous article we read about the creating a Metadata in a post in this article you learn how update a Metadata in the post, in the wordpress for update the Metadata using the update_post_meta() function. So this function will update a piece of metadata attached to a post specified. This function will create meta key if it does not already exist.

<?php update_post_meta( $post_id, $meta_key, $meta_value, $prev_value ); ?>

The following parameters are accept by this funtion:

  •  $post_id — This is use for the ID of the post to update metadata.
  •  $meta_key — This is use for the name of the metadata field.
  •  $meta_value — This is use for the value of the metadata field.
  •  $prev_value — This is use for the old value of the metadata field to update. For the differentiate between several fields with the same key and is an optional field.

Let’s suppose, in this example update the price on your product from earlier as follows:

update_post_meta( 571, ‘prowp_price’, ‘77.15’ );

The above example you are update the product price which is exist in the 571 ID of the post.