get_post_meta() WordPress Function

The get_post_meta() function is used to retrieve metadata from a given post. It can be used to retrieve data such as the post author, post date, or post content.

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

Retrieves a post meta field for the given post ID.


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) Whether to return a single value. This parameter has no effect if $key is not specified.

Default value: false


Top ↑

Return

(mixed) 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.


Top ↑

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.

Top ↑

Source

File: wp-includes/post.php

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


Top ↑

Changelog

Changelog
VersionDescription
1.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More