wppaste
WordPress

get_metadata_by_mid( string $meta_type, int $meta_id ): stdClass|false

Since
3.3.0
Source
wp-includes/meta.php:821
Retrieves 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 'blog', '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 false if the metadata doesn't exist.
  • $meta_keystring

    The meta key.
  • $meta_valuemixed

    The unserialized meta value.
  • $meta_idstring

    Optional. The meta ID when the meta type is any value except 'user'.
  • $umeta_idstring

    Optional. The meta ID when the meta type is 'user'.
  • $blog_idstring

    Optional. The object ID when the meta type is 'blog'.
  • $post_idstring

    Optional. The object ID when the meta type is 'post'.
  • $comment_idstring

    Optional. The object ID when the meta type is 'comment'.
  • $term_idstring

    Optional. The object ID when the meta type is 'term'.
  • $user_idstring

    Optional. 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

Reaches the database via ->get_row().

Scaling
Constant

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

Instructions
5–61

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

Plugin surface
1 hook

Third-party callbacks on 'get_{$meta_type}_metadata_by_mid' 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_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.

WhenInstructionsCalls it makes
always5–7none
$meta_id12–16floor()
$meta_id == false21floor(), _get_meta_table()
$meta_id == false && $check !== null32floor(), _get_meta_table(), apply_filters()
$meta_id == false && $check === null51–54floor(), _get_meta_table(), apply_filters(), ->prepare(), ->get_row()
$meta_id == false && $check === null && !empty($meta) && isset($meta)60–61floor(), _get_meta_table(), apply_filters(), ->prepare(), ->get_row(), maybe_unserialize()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev675–619
8.5675–619
8.4675–6192 fewer instructions than PHP 8.3
8.3695–639
8.2695–639
8.1695–639
7.4695–639

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:

  1. apply_filters( get_{$meta_type}_metadata_by_mid )filterline 858 (+37 into the body)

    Short-circuits the return value when fetching a meta field by meta ID.

Uses · 3

Used by · 7

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	 * (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:	 *	 *  - `get_blog_metadata_by_mid`	 *  - `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.

  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.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.