update_post_meta( int $post_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' ): int|bool
- Since
- 1.5.0
- Source
wp-includes/post.php:2739
Save a custom field on a post with update_post_meta(), updating the existing row or adding the key when the post does not have it yet. It returns the new meta ID when the key was added, true on a change, and false when the value is unchanged or the update fails. $prev_value targets one row when a key holds several values; revision IDs are redirected to the parent post.
Description
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().
For historical reasons both the meta key and the meta value are expected to be "slashed" (slashes escaped) on input.
Compatibility
- WordPress
- since 1.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$post_idint- Post ID.
$meta_keystring- Metadata key.
$meta_valuemixed- Metadata value. Must be serializable if non-scalar.
$prev_valuemixedoptional- Previous value to check before updating.
If specified, only update existing metadata entries with this value. Otherwise, update all entries. Default empty.Default:''
Return value
int|bool- Meta ID if the key didn't exist, true on successful update, false on failure or if the value passed to the function is the same as the one that is already in the database.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Set a custom field, then change it
The return value differs between the first write and later ones, which trips up code that checks it.
delete_post_meta( 4, 'event_date' );
$first = update_post_meta( 4, 'event_date', '2026-09-01' );
$second = update_post_meta( 4, 'event_date', '2026-09-08' );
$same = update_post_meta( 4, 'event_date', '2026-09-08' );
echo 'first write: ', var_export( $first, true ), " (the new meta_id)\n";
echo 'changed value: ', var_export( $second, true ), "\n";
echo 'unchanged value:', var_export( $same, true ), " (false, not an error)\n";
echo 'stored: ', get_post_meta( 4, 'event_date', true );Writing the value it already holds returns false, so never treat false as failure.
Replace one value among several with $prev_value
When a key holds several rows, $prev_value picks the one to overwrite instead of replacing them all.
delete_post_meta( 5, 'badge' );
add_post_meta( 5, 'badge', 'silver' );
add_post_meta( 5, 'badge', 'bronze' );
// Targets one row; without $prev_value both rows would collapse into one.
update_post_meta( 5, 'badge', 'gold', 'silver' );
print_r( get_post_meta( 5, 'badge' ) );Omitting $prev_value on a multi-row key deletes the extras, which is rarely what you want.
Performance profile
How much work a call to update_post_meta() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Constant
- Instructions
- 17–18
- Plugin surface
- None
- Called by
- 38
Only touches the object cache, via wp_cache_delete().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 18.
Nothing here hands control to plugin code.
38 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below update_post_meta() - serializeserialisation
maybe_serialize()one call below update_post_meta() - cacheobject cache
wp_cache_delete()one call below update_post_meta()
Further down the call graph this can also reach query, option and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost update_post_meta() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 17–18 | wp_is_post_revision(), update_metadata() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 18 instructions, 17–18 executed per call, 1 branch. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 2
- wp_is_post_revision()Determines if the specified post is a revision.
- update_metadata()Updates metadata for the specified object. If no value already exists for the specified object ID and metadata key, the metadata will be added.
Used by · 38
- Custom_Background::ajax_background_add()Handles Ajax request for adding custom background context to an attachment.
- Custom_Background::handle_upload()Handles an Image upload for the background image.
- Custom_Background::wp_set_background_image()
- Custom_Image_Header::ajax_header_add()Given an attachment ID for a header image, updates its "last used" timestamp to now.
- Custom_Image_Header::customize_set_last_used()Updates the last-used postmeta on a header image attachment after saving a new header image via the Customizer.
- Custom_Image_Header::set_header_image()Chooses a header image, selected from existing uploaded and default headers, or provides an array of uploaded header data (either new, or from media library).
- WP_Customize_Manager::dismiss_user_auto_draft_changesets()Dismisses all of the current user's auto-drafts (other than the present one).
- WP_Customize_Manager::import_theme_starter_content()Imports theme starter content into the customized state.
- WP_Customize_Manager::refresh_changeset_lock()Refreshes changeset lock with the current time if current user edited the changeset before.
- WP_Customize_Manager::set_changeset_lock()Marks the changeset post as being currently edited by the current user.
- WP_Embed::shortcode()The do_shortcode() callback function.
- WP_REST_Attachments_Controller::create_item()Creates a single attachment.
Show all 38
- WP_REST_Attachments_Controller::edit_media_item()Applies edits to a media item and creates a new attachment record.
- WP_REST_Attachments_Controller::update_item()Updates a single attachment.
- WP_REST_Posts_Controller::handle_template()Sets the template for a post.
- _wp_check_split_terms_in_menus()Checks menu items when a term gets split to see if any of them need to be updated.
- _wp_privacy_account_request_confirmed()Updates log when privacy request is confirmed.
- _wp_privacy_completed_request()Marks a request as completed by the admin and logs the current timestamp.
- _wp_privacy_send_erasure_fulfillment_notification()Notifies the user when their erasure request is fulfilled.
- _wp_privacy_send_request_confirmation_notification()Notifies the site administrator via email when a request is confirmed.
- bulk_edit_posts()Processes the post data for the bulk editing of posts.
- edit_post()Updates an existing post with values provided in `$_POST`.
- media_upload_form_handler()Handles form submissions for the legacy media uploader.
- set_post_thumbnail()Sets the post thumbnail (featured image) for the given post.
- update_attached_file()Updates attachment file path based on attachment ID.
- wp_ajax_save_attachment()Handles updating attachment attributes via AJAX.
- wp_ajax_upload_attachment()Handles uploading attachments via AJAX.
- wp_ajax_wp_remove_post_lock()Handles removing a post lock via AJAX.
- wp_generate_attachment_metadata()Generates attachment meta data and create image sub-sizes for images.
- wp_insert_post()Inserts or update a post.
- wp_privacy_generate_personal_data_export_file()Generate the personal data export file.
- wp_privacy_process_personal_data_export_page()Intercept personal data exporter page Ajax responses in order to assemble the personal data export file.
- wp_restore_image()Restores the metadata for a given attachment.
- wp_restore_post_revision()Restores a post to the specified revision.
- wp_save_image()Saves image to post, along with enqueued changes in `$_REQUEST['history']`.
- wp_set_post_lock()Marks the post as currently being edited by the current user.
- wp_update_attachment_metadata()Updates metadata for an attachment.
- wp_update_nav_menu_item()Saves the properties of a menu item or create a new one.
Source code
function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) { // Make sure meta is updated for the post, not for 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 );}Changelog
Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.