Adding Metadata in WordPress

To add new post metadata this add_post_meta()  simple function is called in WordPress. A piece of meta data to the post will attach by this function this specified as follows:

<?php add_post_meta( $post_id, $meta_key, $meta_value, $unique ); ?>

There are four parameters accepts by this function:

                1. $post_id — It use for the ID of the post to add metadata.

                2. $meta_key — It use for the name of the metadata field.

                3. $meta_value — It use the value of the metadata field.

                4. $unique — It use as a value identifying that whether or not the key should be unique. The default value is false.

How to use this function the add_post_meta()  in the WordPress, For your products you can use it to add some metadata.

add_post_meta( 71, ‘prowp_price’, ‘57.77’, true );

Above the code example adds a metadata entry called prowp_price with a value of 57.77 to product ID 71. The $unique value to true also set, it means that there cannot be multiple entries for the prowp_price field on this product. After this if you edit the product in WordPress, Now you will see value in the custom fields meta box  and a prowp_price field.

NOTE On the Post Edit screento prevent metadata keys from appearing in the Custom Fields meta box, Simple prefix the meta key with an underscore like _prowp_price in the metadata. It will hide the data from the user and is common practice when creating custom meta boxes in WordPress.