add_metadata() wordpress function and usage This text is not used because the data attribute has a value

add_metadata() wordpress function and usage

Home / WordPress / WordPress Wiki / add_metadata() wordpress function and usage

add_metadata( string $meta_type, int $object_id, string $meta_key, mixed $meta_value, bool $unique = false )
Adds metadata for the specified object.



(string) (Required) Type of object metadata is for. Accepts ‘post’, ‘comment’, ‘term’, ‘user’, or any other object type with an associated meta table.


(int) (Required) ID of the object metadata is for.


(string) (Required) Metadata key.


(mixed) (Required) Metadata value. Must be serializable if non-scalar.


(bool) (Optional) Whether the specified metadata key should be unique for the object. If true, and the object already has a value for the specified metadata key, no change will be made.

Default value: false


(int|false) The meta ID on success, false on failure.

Sample Usage


Add an additional meta value for comments. Here location is the meta key $location is the variable holds its value.

function add_comment_location($comment_id, $location) {
    return add_metadata( 'comment', $comment_id, 'location', wp_slash( $location ) );


0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments