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.

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. Meta ID if the key didn't exist, 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 removing.
Default: ''

Code of update post meta: wp-includes/post.php VER 5.0.3

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