Retrieving an object’s metadata using get_metadata in WordPress

The get_metadata function in WordPress is used to retrieve metadata for a specified object. This function can be useful for retrieving additional information about a post, user, comment, or term in WordPress. It allows developers to access and display specific metadata associated with these objects, such as custom fields, user meta, or term meta.

By using the get_metadata function, developers can access and display relevant metadata for various objects within their WordPress website. This can be particularly useful for creating custom templates, displaying additional information on posts or user profiles, or for creating custom functionality based on specific metadata values.

Parameters of the WordPress get_metadata function

The get_metadata function accepts the following 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, default: ”): Metadata key. If not specified, retrieve all metadata for the specified object.
  • $single (bool, optional, default: false): If true, return only the first value of the specified $meta_key. This parameter has no effect if $meta_key is not specified.

Value returned by the function

The get_metadata function returns the following:

  • An array of values if $single is false.
  • The value of the meta field if $single is true.
  • False for an invalid $object_id (non-numeric, zero, or negative value), or if $meta_type is not specified.
  • An empty string if a valid but non-existing object ID is passed.


How to get post metadata using get_metadata function

$post_id = 123;
$meta_key = 'custom_field';
$single = true;
$metadata = get_metadata( 'post', $post_id, $meta_key, $single );

This code snippet retrieves the metadata value of a specific post with ID 123 for the custom field named ‘custom_field’ using the get_metadata function. The $single parameter is set to true to return a single value.

How to get user metadata using get_metadata function

$user_id = 456;
$meta_key = 'user_role';
$single = true;
$metadata = get_metadata( 'user', $user_id, $meta_key, $single );

This code snippet fetches the metadata value of a specific user with ID 456 for the user role using the get_metadata function. The $single parameter is set to true to return a single value.

How to check if post metadata exists using get_metadata function

$post_id = 789;
$meta_key = 'featured_image';
$metadata = get_metadata( 'post', $post_id, $meta_key );
if ( !empty( $metadata ) ) {
 echo 'Featured image exists for post ID 789';
} else {
 echo 'No featured image found for post ID 789';

This code snippet checks if the metadata for a featured image exists for a specific post with ID 789 using the get_metadata function. If the metadata is not empty, it outputs a message indicating the existence of the featured image; otherwise, it displays a message indicating no featured image found.


In conclusion, the get_metadata function is a powerful tool for retrieving metadata information from various types of files. Whether it’s extracting EXIF data from images or accessing ID3 tags from audio files, this function provides a convenient and efficient way to access important information about a file. By utilizing this function, developers can streamline their workflow and improve the user experience by providing accurate and relevant metadata. With its flexibility and ease of use, the get_metadata function is a valuable addition to any developer’s toolkit.

Related WordPress Functions