W3cubDocs

/WordPress

get_post_meta( int $post_id, string $key = '', bool $single = false )

Retrieves a post meta field for the given post ID.

Description

Parameters

$post_id

(int) (Required) Post ID.

$key

(string) (Optional) The meta key to retrieve. By default, returns data for all keys.

Default value: ''

$single

(bool) (Optional) If true, returns only the first value for the specified meta key. This parameter has no effect if $key is not specified.

Default value: false

Return

(mixed) Will be an array if $single is false. Will be value of the meta field if $single is true.

Source

File: wp-includes/post.php

function get_post_meta( $post_id, $key = '', $single = false ) {
	return get_metadata( 'post', $post_id, $key, $single );
}

Changelog

Version Description
1.5.0 Introduced.

More Information

  • 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.

© 2003–2019 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_post_meta