Function: update_post_meta

update_post_meta( integer $post_id, string $meta_key, mixed $meta_value, mixed $prev_value )

Updates a post meta field based on the given post ID.

Shortcut: upm

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.

Use the $prev_value parameter to differentiate between meta fields with the same key and post ID.

If the meta field for the post does not exist, it will be added and its ID returned.

Can be used in place of add_post_meta().

Parameters

Name Type(s) Default Value Description
$post_id integer

Post ID.

$meta_key string

Metadata key.

$meta_value mixed

Metadata value. Must be serializable if non-scalar.

$prev_value mixed ''

Optional. Previous value to check before updating.

Returns

integer | boolean

The new meta field ID if a field with the given key didn't exist and was therefore added, true on successful update, false on failure.

WordPress Developer Newsletter

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