Function: add_post_meta

add_post_meta( integer $post_id, string $meta_key, mixed $meta_value, boolean $unique )

Adds a meta field to the given post.

Shortcut: apm

You can use above key combination to trigger the associated snippet for this function in the WordPress Snippets for Visual Studio Code extension. You can also use this shortcut to search for this function on this website.

Post meta data is called "Custom Fields" on the Administration Screen.


Name Type(s) Default Value Description
$post_id integer

Post ID.

$meta_key string

Metadata name.

$meta_value mixed

Metadata value. Must be serializable if non-scalar.

$unique boolean false

Optional. Whether the same key should not be added. Default false.


integer | false

Meta ID on success, false on failure.

WordPress Developer Newsletter

Stay informed of new chapter releases, important WordPress API updates and more.