Using WordPress ‘get_metadata_raw()’ PHP function

The get_metadata_raw() WordPress PHP function retrieves raw metadata value for the specified object.

Usage

get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false );

Parameters

  • $meta_type (string) (Required): Type of object metadata is for. Accepts ‘post’, ‘comment’, ‘term’, ‘user’, or any other object type with an associated meta table.
  • $object_id (int) (Required): ID of the object metadata is for.
  • $meta_key (string) (Optional): Metadata key. If not specified, retrieve all metadata for the specified object. Default: ”
  • $single (bool) (Optional): If true, return only the first value of the specified $meta_key. This parameter has no effect if $meta_key is not specified. Default: false

More information

See WordPress Developer Resources: get_metadata_raw()

Examples

Get post metadata

Retrieve metadata for a specific post.

$post_id = 42;
$metadata = get_metadata_raw( 'post', $post_id );

Get comment metadata

Retrieve metadata for a specific comment.

$comment_id = 123;
$metadata = get_metadata_raw( 'comment', $comment_id );

Get user metadata

Retrieve metadata for a specific user.

$user_id = 5;
$metadata = get_metadata_raw( 'user', $user_id );

Get term metadata

Retrieve metadata for a specific term.

$term_id = 22;
$metadata = get_metadata_raw( 'term', $term_id );

Get single metadata value

Retrieve the first value of a specific metadata key for a post.

$post_id = 42;
$meta_key = 'custom_key';
$single_value = get_metadata_raw( 'post', $post_id, $meta_key, true );