WordPress at a glance

update_post_meta() WP 1.0

Update post meta field based on post ID.

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().

Works based on: update_metadata()
✈ 1 time = 0.001327s = very slow | 50000 times = 42.23s = very slow | PHP 7.0.8, WP 4.6.1

No Hooks.


Int/true/false. 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.


update_post_meta( $post_id, $meta_key, $meta_value, $prev_value );
$post_id(int) (required)
Post ID.
$meta_key(string) (required)
Metadata key.
$meta_value(mixed) (required)
Metadata value. Must be serializable if non-scalar.
Previous value to check before updating.
Default: ''

Code of update post meta: wp-includes/post.php WP 5.2.1

function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
	// Make sure meta is added to the post, not a revision.
	$the_post = wp_is_post_revision( $post_id );
	if ( $the_post ) {
		$post_id = $the_post;

	return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );

Related Functions

From tag: update

More from tag: Metadatas (any)

No comments
    Hello, !     Log In . Register