wppaste
WordPress

update_metadata_by_mid( string $meta_type, int $meta_id, string $meta_value, string|false $meta_key = false ): bool

Since
3.3.0
Source
wp-includes/meta.php:861
Updates metadata by meta ID.

Compatibility

WordPress
since 3.3.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.
$meta_idint
ID for a specific meta row.
$meta_valuestring
Metadata value. Must be serializable if non-scalar.
$meta_keystring|falseoptional
You can provide a meta key to update it. Default false.Default: false

Return value

bool
True on successful update, false on failure.

Performance profile

How much work a call to update_metadata_by_mid() 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 ->update().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
7–133

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

Plugin surface
5 hooks

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

Called by
4

4 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->update()called directly
  • querycontent queryget_term()one call below update_metadata_by_mid()

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 · 11 distinct outcomes

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

WhenInstructionsCalls it makes
always7–9none
$meta_id14–18floor()
$meta_id == false23floor(), _get_meta_table()
$meta_id == false && $check !== null46–47floor(), _get_meta_table(), sanitize_key(), apply_filters()
$meta_id == false && $check === null51–58floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid()
$meta_id == false && $check === null && $meta_type !== "post"101–102floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid(), get_object_subtype(), sanitize_meta(), maybe_serialize(), do_action(), ->update()
$meta_id == false && $check === null && $meta_type === "post"108–109floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid(), get_object_subtype(), sanitize_meta(), maybe_serialize(), do_action(), do_action(), ->update()
$meta_id == false && $check === null && $meta_type !== "post"118–119floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid(), get_object_subtype(), sanitize_meta(), maybe_serialize(), do_action(), ->update(), wp_cache_delete(), do_action()
$meta_id == false && $check === null125–126floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid(), get_object_subtype(), sanitize_meta(), maybe_serialize(), do_action(), ->update(), wp_cache_delete(), do_action(), do_action()
$meta_id == false && $check === null125–126floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid(), get_object_subtype(), sanitize_meta(), maybe_serialize(), do_action(), do_action(), ->update(), wp_cache_delete(), do_action()
$meta_id == false && $check === null && $meta_type === "post"132–133floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid(), get_object_subtype(), sanitize_meta(), maybe_serialize(), do_action(), do_action(), ->update(), wp_cache_delete(), do_action(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1447–13313
8.51447–13313
8.41447–133132 fewer instructions than PHP 8.3
8.31467–13513
8.21467–13513
8.11467–13513
7.41467–13513

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_by_mid() runs, in this order:

  1. apply_filters( update_{$meta_type}_metadata_by_mid )filterline 903 (+42 into the body)

    Short-circuits updating metadata of a specific type by meta ID.

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

    Fires immediately before updating metadata of a specific type.

  3. do_action( update_postmeta )actionline 946 (+85 into the body)

    Fires immediately before updating a post's metadata.

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

    Fires immediately after updating metadata of a specific type.

  5. do_action( updated_postmeta )actionline 963 (+102 into the body)

    Fires immediately after updating a post's metadata.

Uses · 9

Used by · 4

Source code

function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {	global $wpdb; 	// Make sure everything is valid.	if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {		return false;	} 	$meta_id = (int) $meta_id;	if ( $meta_id <= 0 ) {		return false;	} 	$table = _get_meta_table( $meta_type );	if ( ! $table ) {		return false;	} 	$column    = sanitize_key( $meta_type . '_id' );	$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 	/**	 * Short-circuits updating metadata of a specific type by meta ID.	 *	 * 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_by_mid`	 *  - `update_comment_metadata_by_mid`	 *  - `update_term_metadata_by_mid`	 *  - `update_user_metadata_by_mid`	 *	 * @since 5.0.0	 *	 * @param null|bool    $check      Whether to allow updating metadata for the given type.	 * @param int          $meta_id    Meta ID.	 * @param mixed        $meta_value Meta value. Must be serializable if non-scalar.	 * @param string|false $meta_key   Meta key, if provided.	 */	$check = apply_filters( "update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key );	if ( null !== $check ) {		return (bool) $check;	} 	// Fetch the meta and go on if it's found.	$meta = get_metadata_by_mid( $meta_type, $meta_id );	if ( $meta ) {		$original_key = $meta->meta_key;		$object_id    = $meta->{$column}; 		/*		 * If a new meta_key (last parameter) was specified, change the meta key,		 * otherwise use the original key in the update statement.		 */		if ( false === $meta_key ) {			$meta_key = $original_key;		} elseif ( ! is_string( $meta_key ) ) {			return false;		} 		$meta_subtype = get_object_subtype( $meta_type, $object_id ); 		// Sanitize the meta.		$_meta_value = $meta_value;		$meta_value  = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );		$meta_value  = maybe_serialize( $meta_value ); 		// Format the data query arguments.		$data = array(			'meta_key'   => $meta_key,			'meta_value' => $meta_value,		); 		// Format the where query arguments.		$where               = array();		$where[ $id_column ] = $meta_id;

Changelog

Introduced in 3.3.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.