get_metadata_by_mid( string $meta_type, int $meta_id ): stdClass|false
- Since
- 3.3.0
- Source
wp-includes/meta.php:791
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.
Return value
stdClass|false- Metadata object, or boolean
falseif the metadata doesn't exist.$meta_keystringThe meta key.$meta_valuemixedThe unserialized meta value.$meta_idstringOptional. The meta ID when the meta type is any value except 'user'.$umeta_idstringOptional. The meta ID when the meta type is 'user'.$post_idstringOptional. The object ID when the meta type is 'post'.$comment_idstringOptional. The object ID when the meta type is 'comment'.$term_idstringOptional. The object ID when the meta type is 'term'.$user_idstringOptional. The object ID when the meta type is 'user'.
Performance profile
How much work a call to get_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
- 5–61
- Plugin surface
- 1 hook
- Called by
- 7
Reaches the database via ->get_row().
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 67.
Third-party callbacks on 'get_{$meta_type}_metadata_by_mid' run inside this call, and their cost is not bounded by anything here.
7 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_unserialize()called directly - sqldatabase query
->get_row()called directly
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_metadata_by_mid() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5–7 | none |
$meta_id | 12–16 | floor() |
$meta_id == false | 21 | floor(), _get_meta_table() |
$meta_id == false && $check !== null | 32 | floor(), _get_meta_table(), apply_filters() |
$meta_id == false && $check === null | 51–54 | floor(), _get_meta_table(), apply_filters(), ->prepare(), ->get_row() |
$meta_id == false && $check === null && !empty($meta) && isset($meta) | 60–61 | floor(), _get_meta_table(), apply_filters(), ->prepare(), ->get_row(), maybe_unserialize() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 67 | 5–61 | 9 | |
| 8.5 | 67 | 5–61 | 9 | |
| 8.4 | 67 | 5–61 | 9 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 69 | 5–63 | 9 | |
| 8.2 | 69 | 5–63 | 9 | |
| 8.1 | 69 | 5–63 | 9 | |
| 7.4 | 69 | 5–63 | 9 |
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 · 1
One hook fires while get_metadata_by_mid() runs, in this order:
- apply_filters( get_{$meta_type}_metadata_by_mid )filterline 827 (+36 into the body)
Short-circuits the return value when fetching a meta field by meta ID.
Uses · 3
- _get_meta_table()Retrieves the name of the metadata table for the specified object type.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- maybe_unserialize()Unserializes data only if it was serialized.
Used by · 7
- delete_metadata_by_mid()Deletes metadata by meta ID.
- get_post_meta_by_id()Returns post meta data by meta ID.
- update_metadata_by_mid()Updates metadata by meta ID.
- wp_ajax_add_meta()Handles adding meta via AJAX.
- wp_ajax_delete_meta()Handles deleting 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 get_metadata_by_mid( $meta_type, $meta_id ) { global $wpdb; 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; } /** * Short-circuits the return value when fetching a meta field 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: * * - `get_post_metadata_by_mid` * - `get_comment_metadata_by_mid` * - `get_term_metadata_by_mid` * - `get_user_metadata_by_mid` * * @since 5.0.0 * * @param stdClass|null $value The value to return. * @param int $meta_id Meta ID. */ $check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id ); if ( null !== $check ) { return $check; } $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) ); if ( empty( $meta ) ) { return false; } if ( isset( $meta->meta_value ) ) { $meta->meta_value = maybe_unserialize( $meta->meta_value ); } return $meta;}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.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.