Getting metadata by ID in WordPress with get_metadata_by_mid

The WordPress get_metadata_by_mid function is a part of the WordPress core that retrieves metadata by its ID. The function is used to fetch metadata for various objects such as posts, comments, users, and terms. It can retrieve metadata from any meta table in the WordPress database that has the standard structure, with columns for meta_id, meta_key, and meta_value.

This function is useful in several scenarios. For instance, when you need to access specific metadata that is associated with a certain ID. It can also be used when you want to retrieve metadata without knowing its key, but only its ID. This function provides a way to directly access metadata using the unique meta ID, which can be beneficial when working with large amounts of metadata.

Parameters Accepted by the get_metadata_by_mid Function

The get_metadata_by_mid function in WordPress accepts two required parameters:

  • $meta_type (string): This parameter specifies the type of object the metadata is associated with. It can be ‘post’, ‘comment’, ‘term’, ‘user’, or any other object type that has a corresponding meta table.
  • $meta_id (integer): This parameter represents the ID of a specific meta row.

Return Value of the get_metadata_by_mid Function

The get_metadata_by_mid function returns either a metadata object (stdClass) or a boolean false if the metadata does not exist. The returned metadata object includes the following:

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

If the function does not accept any parameters, it will be explicitly stated.

Examples

How to Retrieve Post Meta Data Using get_metadata_by_mid Function

$post_meta_id = 123; // Assume 123 is the ID of the post meta data
$post_meta_data = get_metadata_by_mid('post', $post_meta_id);

if ($post_meta_data) {
 echo '<p>Meta Key: ' . $post_meta_data->meta_key . '</p>';
 echo '<p>Meta Value: ' . $post_meta_data->meta_value . '</p>';
} else {
 echo '<p>No meta data found for the given ID.</p>';
}

This code snippet retrieves the meta data of a post by its ID using the get_metadata_by_mid function. If the meta data exists, it displays the meta key and meta value. If not, it displays a message saying that no meta data was found for the given ID.

How to Retrieve User Meta Data Using get_metadata_by_mid Function

$user_meta_id = 456; // Assume 456 is the ID of the user meta data
$user_meta_data = get_metadata_by_mid('user', $user_meta_id);

if ($user_meta_data) {
 echo '<p>Meta Key: ' . $user_meta_data->meta_key . '</p>';
 echo '<p>Meta Value: ' . $user_meta_data->meta_value . '</p>';
} else {
 echo '<p>No meta data found for the given ID.</p>';
}

This code snippet retrieves the meta data of a user by its ID using the get_metadata_by_mid function. If the meta data exists, it displays the meta key and meta value. If not, it displays a message saying that no meta data was found for the given ID.

How to Retrieve Comment Meta Data Using get_metadata_by_mid Function

$comment_meta_id = 789; // Assume 789 is the ID of the comment meta data
$comment_meta_data = get_metadata_by_mid('comment', $comment_meta_id);

if ($comment_meta_data) {
 echo '<p>Meta Key: ' . $comment_meta_data->meta_key . '</p>';
 echo '<p>Meta Value: ' . $comment_meta_data->meta_value . '</p>';
} else {
 echo '<p>No meta data found for the given ID.</p>';
}

This code snippet retrieves the meta data of a comment by its ID using the get_metadata_by_mid function. If the meta data exists, it displays the meta key and meta value. If not, it displays a message saying that no meta data was found for the given ID.

Conclusion

The get_metadata_by_mid function in WordPress is a specific tool designed to retrieve metadata by its meta ID. This function can be applied to posts, users, comments, or terms, making it a versatile tool in the WordPress ecosystem. It primarily serves to facilitate the process of fetching metadata in a WordPress database, which can be used for a variety of purposes such as customizing site features, enhancing user experience, or improving site management and organization.

Related WordPress Functions