Retrieving post metadata in WordPress using get_post_meta

The get_post_meta function in WordPress is used to retrieve the value of a custom field for a specific post. This can be useful for displaying additional information about a post, such as author details, post categories, or any other custom data that has been added to the post.

By using the get_post_meta function, developers can access and display this custom data in their WordPress themes or plugins, allowing for greater flexibility and customization of the content displayed on the website.

  • Retrieve custom field values
  • Display additional information about a post
  • Enhance the flexibility and customization of WordPress themes and plugins

Parameters accepted by the WordPress get_post_meta function

  • $post_id (int) – required. Description: Post ID.
  • $key (string) – optional. Default value: ”. Description: The meta key to retrieve. By default, returns data for all keys.
  • $single (bool) – optional. Default value: false. Description: Whether to return a single value. This parameter has no effect if $key is not specified.

Value returned by the WordPress get_post_meta function

The 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 $post_id (non-numeric, zero, or negative value).
  • An empty string if a valid but non-existing post ID is passed.


How to get a single post meta value

$post_id = 123;
$meta_value = get_post_meta( $post_id, 'custom_field', true );

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

How to get all post meta values

$post_id = 456;
$meta_values = get_post_meta( $post_id );

This code snippet retrieves all meta values for the post with ID 456 using the get_post_meta function. Since the third parameter is not provided, it defaults to false and returns an array of all meta values.

How to check if a specific post meta key exists

$post_id = 789;
if ( metadata_exists( 'post', $post_id, 'custom_field' ) ) {
 // Meta key 'custom_field' exists for the post
 // Add your logic here

This code snippet checks if the meta key ‘custom_field’ exists for the post with ID 789 using the metadata_exists function. If the key exists, a specific logic can be executed based on the condition.


In conclusion, the get_post_meta function is an useful utility for retrieving custom field data from WordPress posts. It allows developers to access and display specific metadata associated with a post, giving them greater control over the content and presentation of their websites. By understanding how to use this function effectively, developers can enhance the functionality and user experience of their WordPress sites. With its flexibility and versatility, get_post_meta is a valuable asset for any WordPress developer.

Related WordPress Functions