Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get post meta


This function returns the values of the custom fields with the specified key from the specified post. It is a wrapper for get_metadata('post'). To return all of the custom fields, see get_post_custom(). See also update_post_meta(), delete_post_meta() and add_post_meta().


<?php $meta_values get_post_meta$post_id$key$single ); ?>


(integer) (required) The ID of the post from which you want the data. Use get_the_ID() or the global $post object's ID property (eg $post->ID) while in The Loop to get the post's ID, or use your sub-loop's post object ID property (eg $my_post_object->ID). (Note: When using a Page to display your posts (set in Settings -> Reading), get_the_ID() and $post->ID will grab the latest Post's ID. To get the containing Page's ID, you will need to use get_queried_object_id().)
Default: None
(string) (optional) A string containing the name of the meta value you want.
Default: None
(boolean) (optional) If set to true then the function will return a single result, as a string. If false, or not set, then the function returns an array of the custom fields. This may not be intuitive in the context of serialized arrays. If you fetch a serialized array with this method you want $single to be true to actually get an unserialized array back. If you pass in false, or leave it out, you will have an array of one, and the value at index 0 will be the serialized string.
Default: false

Return Value

  • If only $id is set it will return all meta values in an associative array.
  • If $single is set to false, or left blank, the function returns an array containing all values of the specified key.
  • If $single is set to true, the function returns the first value of the specified key (not in an array)

If there is nothing to return the function will return an empty array unless $single has been set to true, in which case an empty string is returned.


Default Usage

Get the meta for all keys:

<?php $meta = get_post_meta( get_the_ID() ); ?>

Get the meta for a single key:

<?php $key_1_values = get_post_meta( 76, 'key_1' ); ?>

show the first value of the specified key inside a loop

$key_1_value = get_post_meta( get_the_ID(), 'key_1', true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
  echo $key_1_value;

For a more detailed example, go to the post_meta Functions Examples page.

Retrieve a Custom Field Thumbnail Url

While you are in the WordPress Loop, you can use this code to retrieve a custom field. In this example, the thumbnail image url is in a custom field named "thumb".

<?php if ( get_post_meta( get_the_ID(), 'thumb', true ) ) : ?>
    <a href="<?php the_permalink() ?>" rel="bookmark">
        <img class="thumb" src="<?php echo get_post_meta( get_the_ID(), 'thumb', true ); ?>" alt="<?php the_title(); ?>" />
<?php endif; ?>


  • Please note that if a db collation is case insensitive (has with suffix _ci) then update_post_meta and delete_post_meta and get_posts() will update/delete/query the meta records with keys that are upper or lower case. However get_post_meta will apparently be case sensitive due to WordPress caching. See https://core.trac.wordpress.org/ticket/18210 for more info. Be careful not to mix upper and lowercase.
  • Uses: get_metadata() to retrieve the metadata.

Change Log

  • Since: 1.5.0

Source Code

get_post_meta() is located in wp-includes/post.php


Custom Fields: the_meta(), get_post_meta(), add_post_meta(), update_post_meta(), delete_post_meta(), get_post_custom(), get_post_custom_values(), get_post_custom_keys() (See Also: post_meta Function Examples)

See also index of Function Reference and index of Template Tags.