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:892
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 'blog', '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
- Scaling
- Constant
- Instructions
- 7–133
- Plugin surface
- 5 hooks
- Called by
- 4
Reaches the database via ->update().
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 144.
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.
4 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()called directly - serializeserialisation
maybe_serialize()called directly - cacheobject cache
wp_cache_delete()called directly - sqldatabase query
->update()called directly - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–9 | none |
$meta_id | 14–18 | floor() |
$meta_id == false | 23 | floor(), _get_meta_table() |
$meta_id == false && $check !== null | 46–47 | floor(), _get_meta_table(), sanitize_key(), apply_filters() |
$meta_id == false && $check === null | 51–58 | floor(), _get_meta_table(), sanitize_key(), apply_filters(), get_metadata_by_mid() |
$meta_id == false && $check === null && $meta_type !== "post" | 101–102 | floor(), _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–109 | floor(), _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–119 | floor(), _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 === null | 125–126 | floor(), _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 === null | 125–126 | floor(), _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–133 | floor(), _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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 144 | 7–133 | 13 | |
| 8.5 | 144 | 7–133 | 13 | |
| 8.4 | 144 | 7–133 | 13 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 146 | 7–135 | 13 | |
| 8.2 | 146 | 7–135 | 13 | |
| 8.1 | 146 | 7–135 | 13 | |
| 7.4 | 146 | 7–135 | 13 |
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:
- apply_filters( update_{$meta_type}_metadata_by_mid )filterline 935 (+43 into the body)
Short-circuits updating metadata of a specific type by meta ID.
- do_action( update_{$meta_type}_meta )actionline 974 (+82 into the body)
Fires immediately before updating metadata of a specific type.
- do_action( update_postmeta )actionline 978 (+86 into the body)
Fires immediately before updating a post's metadata.
- do_action( updated_{$meta_type}_meta )actionline 991 (+99 into the body)
Fires immediately after updating metadata of a specific type.
- do_action( updated_postmeta )actionline 995 (+103 into the body)
Fires immediately after updating a post's metadata.
Uses · 9
- _get_meta_table()Retrieves the name of the metadata table for the specified object type.
- sanitize_key()Sanitizes a string key.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_metadata_by_mid()Retrieves metadata by meta ID.
- get_object_subtype()Returns the object subtype for a given object ID of a specific type.
- sanitize_meta()Sanitizes meta value.
- maybe_serialize()Serializes data, if needed.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_cache_delete()Removes the cache contents matching key and group.
Used by · 4
- update_meta()Updates post meta data by meta ID.
- wp_ajax_add_meta()Handles adding meta via AJAX.
- wp_xmlrpc_server::set_custom_fields()Sets custom fields for post.
- wp_xmlrpc_server::set_term_custom_fields()Sets custom fields for a term.
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 * (blog, 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_blog_metadata_by_mid` * - `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.
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/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.