函数文档

get_metadata_by_mid()

💡 云策文档标注

概述

get_metadata_by_mid() 函数用于通过元数据 ID 检索元数据对象。它接受元类型和元 ID 作为参数,返回一个包含元键、元值等属性的对象,若元数据不存在则返回 false。

关键要点

  • 参数:$meta_type(字符串,必需,指定元数据对象类型,如 'post'、'user' 等)和 $meta_id(整数,必需,指定元数据行的 ID)。
  • 返回值:返回 stdClass 对象(包含 meta_key、meta_value、meta_id 等属性)或 false(如果元数据不存在)。
  • 功能:从数据库中查询指定元 ID 的元数据行,并自动反序列化 meta_value。
  • Hook:提供 apply_filters( "get_{$meta_type}_metadata_by_mid", stdClass|null $value, int $meta_id ) 用于短路返回值。
  • 相关函数:与 update_metadata_by_mid()、delete_metadata_by_mid() 等函数配合使用,用于元数据管理。

代码示例

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;
    }

    $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;
}

注意事项

  • 元类型 $meta_type 必须有效,否则函数返回 false。
  • 元 ID $meta_id 必须是正整数,否则函数返回 false。
  • 返回的对象属性包括 meta_key、meta_value、meta_id(或 umeta_id 对于 'user' 类型)以及对应的对象 ID(如 post_id、user_id 等)。
  • 函数内部使用 wpdb::prepare() 进行 SQL 查询安全防护,开发者无需额外处理。

📄 原文内容

Retrieves metadata by meta ID.

Parameters

$meta_typestringrequired
Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 'user', or any other object type with an associated meta table.
$meta_idintrequired
ID for a specific meta row.

Return

stdClass|false Metadata object, or boolean false if the metadata doesn’t exist.

  • meta_key string
    The meta key.
  • meta_value mixed
    The unserialized meta value.
  • meta_id string
    Optional. The meta ID when the meta type is any value except 'user'.
  • umeta_id string
    Optional. The meta ID when the meta type is 'user'.
  • blog_id string
    Optional. The object ID when the meta type is 'blog'.
  • post_id string
    Optional. The object ID when the meta type is 'post'.
  • comment_id string
    Optional. The object ID when the meta type is 'comment'.
  • term_id string
    Optional. The object ID when the meta type is 'term'.
  • user_id string
    Optional. The object ID when the meta type is 'user'.

Source

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;
}

Hooks

apply_filters( “get_{$meta_type}_metadata_by_mid”, stdClass|null $value, int $meta_id )

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

Changelog

Version Description
3.3.0 Introduced.