Using WordPress ‘get_metadata_by_mid()’ PHP function

The get_metadata_by_mid() WordPress PHP function retrieves metadata by meta ID for a specified object type.

Usage

get_metadata_by_mid( $meta_type, $meta_id );

Custom example:

// Retrieves post metadata with meta ID 12
$metadata = get_metadata_by_mid( 'post', 12 );

Parameters

  • $meta_type (string) – Type of object metadata is for. Accepts ‘post’, ‘comment’, ‘term’, ‘user’, or any other object type with an associated meta table.
  • $meta_id (int) – ID for a specific meta row.

More information

See WordPress Developer Resources: get_metadata_by_mid()

Examples

Retrieve Post Metadata

Retrieve metadata for a post with meta ID 42:

$meta_id = 42;
$post_meta = get_metadata_by_mid( 'post', $meta_id );

Retrieve Comment Metadata

Retrieve metadata for a comment with meta ID 5:

$meta_id = 5;
$comment_meta = get_metadata_by_mid( 'comment', $meta_id );

Retrieve Term Metadata

Retrieve metadata for a term with meta ID 10:

$meta_id = 10;
$term_meta = get_metadata_by_mid( 'term', $meta_id );

Retrieve User Metadata

Retrieve metadata for a user with meta ID 7:

$meta_id = 7;
$user_meta = get_metadata_by_mid( 'user', $meta_id );

Retrieve Custom Object Type Metadata

Retrieve metadata for a custom object type ‘product’ with meta ID 15:

$meta_id = 15;
$custom_meta = get_metadata_by_mid( 'product', $meta_id );

Tagged in

Leave a Comment

Your email address will not be published. Required fields are marked *