wppaste
WordPress

update_metadata( string $meta_type, int $object_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' ): int|bool

Since
2.9.0
Source
wp-includes/meta.php:182
Updates metadata for the specified object. If no value already exists for the specified object ID and metadata key, the metadata will be added.

Compatibility

WordPress
since 2.9.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

$meta_typestring
Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', or any other object type with an associated meta table.
$object_idint
ID of the object metadata is for.
$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 string.Default: ''

Return value

int|bool
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 or if the value passed to the function is the same as the one that is already in the database.

Performance profile

How much work a call to update_metadata() 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
Heavy

Reaches the database via ->get_col().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
8–141

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 196.

Plugin surface
5 hooks

Third-party callbacks on 'update_{$meta_type}_metadata', 'update_{$meta_type}_meta', 'update_postmeta' run inside this call, and their cost is not bounded by anything here.

Called by
7

7 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • serializeserialisationmaybe_serialize()called directly
  • cacheobject cachewp_cache_delete()called directly
  • sqldatabase query->get_col()called directly
  • querycontent queryget_term()one call below update_metadata()

Further down the call graph this can also reach 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 · 15 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost update_metadata() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8–11none
$object_id16absint()
$object_id21absint(), _get_meta_table()
$object_id && $check !== null67–68absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters()
$object_id && $check === null && empty($prev_value) && is_countable() && $meta_value === false84–85absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), get_metadata_raw(), is_countable()
$object_id && $check === null && !empty($prev_value) && empty($meta_ids)92–93absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), ->prepare(), ->get_col(), add_metadata()
$object_id && $check === null && empty($prev_value) && empty($meta_ids)102–109absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), get_metadata_raw(), is_countable(), ->prepare(), ->get_col(), add_metadata()
$object_id && $check === null && !empty($meta_ids)109–111absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), ->prepare(), ->get_col(), maybe_serialize(), compact(), ->update()
$object_id && $check === null && !empty($prev_value) && !empty($meta_ids)115–117absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), ->prepare(), ->get_col(), maybe_serialize(), compact(), maybe_serialize(), ->update()
$object_id && $check === null && !empty($meta_ids)116–119absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), ->prepare(), ->get_col(), maybe_serialize(), compact(), ->update(), wp_cache_delete()
$object_id && $check === null && empty($prev_value) && !empty($meta_ids)119–127absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), get_metadata_raw(), is_countable(), ->prepare(), ->get_col(), maybe_serialize(), compact(), ->update()
$object_id && $check === null && !empty($prev_value) && !empty($meta_ids)122–125absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), ->prepare(), ->get_col(), maybe_serialize(), compact(), maybe_serialize(), ->update(), wp_cache_delete()
3 further outcomes, up to 141 instructions
$object_id && $check === null && !empty($meta_ids)125–133absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), get_metadata_raw(), is_countable(), ->prepare(), ->get_col(), maybe_serialize(), compact(), maybe_serialize(), ->update()
$object_id && $check === null && empty($prev_value) && !empty($meta_ids)126–135absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), get_metadata_raw(), is_countable(), ->prepare(), ->get_col(), maybe_serialize(), compact(), ->update(), wp_cache_delete()
$object_id && $check === null && !empty($meta_ids)132–141absint(), _get_meta_table(), get_object_subtype(), sanitize_key(), wp_unslash(), wp_unslash(), sanitize_meta(), apply_filters(), get_metadata_raw(), is_countable(), ->prepare(), ->get_col(), maybe_serialize(), compact(), maybe_serialize(), ->update(), wp_cache_delete()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1968–14120
8.51968–14120
8.41968–141202 fewer instructions than PHP 8.3
8.31988–14320
8.21988–14320
8.11988–14320
7.41988–14320

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.

Hooks and filters fired · 5

5 hooks fire while update_metadata() runs, in this order:

  1. apply_filters( update_{$meta_type}_metadata )filterline 235 (+53 into the body)

    Short-circuits updating metadata of a specific type.

  2. do_action( update_{$meta_type}_meta )actionline 290 (+108 into the body)

    Fires immediately before updating metadata of a specific type.

  3. do_action( update_postmeta )actionline 304 (+122 into the body)

    Fires immediately before updating a post's metadata.

  4. do_action( updated_{$meta_type}_meta )actionline 336 (+154 into the body)

    Fires immediately after updating metadata of a specific type.

  5. do_action( updated_postmeta )actionline 350 (+168 into the body)

    Fires immediately after updating a post's metadata.

Uses · 13

Show all 13

Used by · 7

Source code

function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_value = '' ) {	global $wpdb; 	if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {		return false;	} 	$object_id = absint( $object_id );	if ( ! $object_id ) {		return false;	} 	$table = _get_meta_table( $meta_type );	if ( ! $table ) {		return false;	} 	$meta_subtype = get_object_subtype( $meta_type, $object_id ); 	$column    = sanitize_key( $meta_type . '_id' );	$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 	// expected_slashed ($meta_key)	$raw_meta_key = $meta_key;	$meta_key     = wp_unslash( $meta_key );	$passed_value = $meta_value;	$meta_value   = wp_unslash( $meta_value );	$meta_value   = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); 	/**	 * Short-circuits updating metadata of a specific type.	 *	 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type	 * (post, comment, term, user, or any other type with an associated meta table).	 * Returning a non-null value will effectively short-circuit the function.	 *	 * Possible hook names include:	 *	 *  - `update_post_metadata`	 *  - `update_comment_metadata`	 *  - `update_term_metadata`	 *  - `update_user_metadata`	 *	 * @since 3.1.0	 *	 * @param null|bool $check      Whether to allow updating metadata for the given type.	 * @param int       $object_id  ID of the object metadata is for.	 * @param string    $meta_key   Metadata key.	 * @param mixed     $meta_value Metadata value. Must be serializable if non-scalar.	 * @param mixed     $prev_value Optional. Previous value to check before updating.	 *                              If specified, only update existing metadata entries with	 *                              this value. Otherwise, update all entries.	 */	$check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );	if ( null !== $check ) {		return (bool) $check;	} 	// Compare existing value to new value if no prev value given and the key exists only once.	if ( empty( $prev_value ) ) {		$old_value = get_metadata_raw( $meta_type, $object_id, $meta_key );		if ( is_countable( $old_value ) && count( $old_value ) === 1 ) {			if ( $old_value[0] === $meta_value ) {				return false;			}		}	} 	$meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) );	if ( empty( $meta_ids ) ) {		return add_metadata( $meta_type, $object_id, $raw_meta_key, $passed_value );	} 	$_meta_value = $meta_value;	$meta_value  = maybe_serialize( $meta_value ); 	$data  = compact( 'meta_value' );	$where = array(		$column    => $object_id,		'meta_key' => $meta_key,

Changelog

Introduced in 2.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/meta.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.