Function Reference/get post meta
Description
This function returns the values of the custom fields with the specified key from the specified post. See also update_post_meta(), delete_post_meta() and add_post_meta().
Usage
<?php $meta_values = get_post_meta($post_id, $key, $single); ?>
Parameters
- $post_id
- (integer) (required) The ID of the post from which you want the data. Use $post->ID to get a post's ID.
- Default: None
- $key
- (string) (required) A string containing the name of the meta value you want.
- Default: None
- $single
- (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 is not intuitive. For example, 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 $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)
The function returns an empty string if the key has not yet been set, regardless of the value of $single.
Examples
Default Usage
<?php $key_1_values = get_post_meta(76, 'key_1'); ?>
Other Example
To retrieve only the first value of a given key:
<?php $key_1_value = get_post_meta(76, 'key_1', true); ?>
For a more detailed example, go to the post_meta Functions Examples page.
Related
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)