get_metadata_by_mid

The timeline below displays how wordpress function get_metadata_by_mid has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.9

/**
 * Retrieves metadata by meta ID.
 *
 * @since 3.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
 *                          or any other object type with an associated meta table.
 * @param int    $meta_id   ID for a specific meta row.
 * @return stdClass|false {
 *     Metadata object, or boolean `false` if the metadata doesn't exist.
 *
 *     @type string $meta_key   The meta key.
 *     @type mixed  $meta_value The unserialized meta value.
 *     @type string $meta_id    Optional. The meta ID when the meta type is any value except 'user'.
 *     @type string $umeta_id   Optional. The meta ID when the meta type is 'user'.
 *     @type string $post_id    Optional. The object ID when the meta type is 'post'.
 *     @type string $comment_id Optional. The object ID when the meta type is 'comment'.
 *     @type string $term_id    Optional. The object ID when the meta type is 'term'.
 *     @type string $user_id    Optional. The object ID when the meta type is 'user'.
 * }
 */
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;
}

WordPress Version: 5.6

/**
 * Retrieves metadata by meta ID.
 *
 * @since 3.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
 *                          or any other object type with an associated meta table.
 * @param int    $meta_id   ID for a specific meta row.
 * @return stdClass|false {
 *     Metadata object, or boolean `false` if the metadata doesn't exist.
 *
 *     @type string $meta_key   The meta key.
 *     @type mixed  $meta_value The unserialized meta value.
 *     @type string $meta_id    Optional. The meta ID when the meta type is any value except 'user'.
 *     @type string $umeta_id   Optional. The meta ID when the meta type is 'user'.
 *     @type string $post_id    Optional. The object ID when the meta type is 'post'.
 *     @type string $comment_id Optional. The object ID when the meta type is 'comment'.
 *     @type string $term_id    Optional. The object ID when the meta type is 'term'.
 *     @type string $user_id    Optional. The object ID when the meta type is 'user'.
 * }
 */
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, `$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.
     *
     * @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;
}

WordPress Version: 5.5

/**
 * Retrieves metadata by meta ID.
 *
 * @since 3.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
 *                          or any other object type with an associated meta table.
 * @param int    $meta_id   ID for a specific meta row.
 * @return stdClass|false {
 *     Metadata object, or boolean `false` if the metadata doesn't exist.
 *
 *     @type string $meta_key   The meta key.
 *     @type mixed  $meta_value The unserialized meta value.
 *     @type string $meta_id    Optional. The meta ID when the meta type is any value except 'user'.
 *     @type string $umeta_id   Optional. The meta ID when the meta type is 'user'.
 *     @type string $post_id    Optional. The object ID when the meta type is 'post'.
 *     @type string $comment_id Optional. The object ID when the meta type is 'comment'.
 *     @type string $term_id    Optional. The object ID when the meta type is 'term'.
 *     @type string $user_id    Optional. The object ID when the meta type is 'user'.
 * }
 */
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 = intval($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, `$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.
     *
     * @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;
}

WordPress Version: 5.4

/**
 * Retrieves metadata by meta ID.
 *
 * @since 3.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
 *                          or any other object type with an associated meta table.
 * @param int    $meta_id   ID for a specific meta row.
 * @return object|false Meta object or false.
 */
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 = intval($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';
    /**
     * Filters whether to retrieve metadata of a specific type by meta ID.
     *
     * The dynamic portion of the hook, `$meta_type`, refers to the meta
     * object type (comment, post, term, or user). Returning a non-null value
     * will effectively short-circuit the function.
     *
     * @since 5.0.0
     *
     * @param mixed $value    The value get_metadata_by_mid() should return.
     * @param int   $meta_id  Meta ID.
     */
    $check = apply_filters("get_{$meta_type}_metadata_by_mid", null, $meta_id);
    if (null !== $check) {
        return $check;
    }
    $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;
}

WordPress Version: 5.0

/**
 * Get meta data by meta ID
 *
 * @since 3.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
 * @param int    $meta_id   ID for a specific meta row
 * @return object|false Meta object or false.
 */
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 = intval($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';
    /**
     * Filters whether to retrieve metadata of a specific type by meta ID.
     *
     * The dynamic portion of the hook, `$meta_type`, refers to the meta
     * object type (comment, post, term, or user). Returning a non-null value
     * will effectively short-circuit the function.
     *
     * @since 5.0.0
     *
     * @param mixed $value    The value get_metadata_by_mid() should return.
     * @param int   $meta_id  Meta ID.
     */
    $check = apply_filters("get_{$meta_type}_metadata_by_mid", null, $meta_id);
    if (null !== $check) {
        return $check;
    }
    $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;
}

WordPress Version: 4.7

/**
 * Get meta data by meta ID
 *
 * @since 3.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
 * @param int    $meta_id   ID for a specific meta row
 * @return object|false Meta object or false.
 */
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 = intval($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;
}

WordPress Version: 4.4

/**
 * Get meta data by meta ID
 *
 * @since 3.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
 * @param int    $meta_id   ID for a specific meta row
 * @return object|false Meta object or false.
 */
function get_metadata_by_mid($meta_type, $meta_id)
{
    global $wpdb;
    if (!$meta_type || !is_numeric($meta_id)) {
        return false;
    }
    $meta_id = absint($meta_id);
    if (!$meta_id) {
        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;
}

WordPress Version: 4.3

/**
 * Get meta data by meta ID
 *
 * @since 3.3.0
 *
 * @global wpdb $wpdb
 *
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
 * @param int    $meta_id   ID for a specific meta row
 * @return object|false Meta object or false.
 */
function get_metadata_by_mid($meta_type, $meta_id)
{
    global $wpdb;
    if (!$meta_type || !is_numeric($meta_id)) {
        return false;
    }
    $meta_id = absint($meta_id);
    if (!$meta_id) {
        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;
}

WordPress Version: 4.0

/**
 * Get meta data by meta ID
 *
 * @since 3.3.0
 *
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
 * @param int $meta_id ID for a specific meta row
 * @return object Meta object or false.
 */
function get_metadata_by_mid($meta_type, $meta_id)
{
    global $wpdb;
    if (!$meta_type || !is_numeric($meta_id)) {
        return false;
    }
    $meta_id = absint($meta_id);
    if (!$meta_id) {
        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;
}

WordPress Version: 3.7

/**
 * Get meta data by meta ID
 *
 * @since 3.3.0
 *
 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user)
 * @param int $meta_id ID for a specific meta row
 * @return object Meta object or false.
 */
function get_metadata_by_mid($meta_type, $meta_id)
{
    global $wpdb;
    if (!$meta_type) {
        return false;
    }
    if (!$meta_id = absint($meta_id)) {
        return false;
    }
    if (!$table = _get_meta_table($meta_type)) {
        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;
}