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:888
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 931 (+43 into the body)
Short-circuits updating metadata of a specific type by meta ID.
- do_action( update_{$meta_type}_meta )actionline 970 (+82 into the body)
Fires immediately before updating metadata of a specific type.
- do_action( update_postmeta )actionline 974 (+86 into the body)
Fires immediately before updating a post's metadata.
- do_action( updated_{$meta_type}_meta )actionline 987 (+99 into the body)
Fires immediately after updating metadata of a specific type.
- do_action( updated_postmeta )actionline 991 (+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.
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 7.1.0 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.