post_custom() WordPress Function

The post_custom() function is a versatile way to get custom fields for a post in WordPress. It can be used to get data for a specific post, or to get an array of all custom fields for a post. You can also specify which custom field you want to get, or you can get all custom fields for a post as an associative array.

post_custom( string $key = '' ) #

Retrieves post custom meta data field.


Parameters

$key

(string)(Optional)Meta data key name.

Default value: ''


Top ↑

Return

(array|string|false) Array of values, or single value if only one element exists. False if the key does not exist.


Top ↑

Source

File: wp-includes/post-template.php

function post_custom( $key = '' ) {
	$custom = get_post_custom();

	if ( ! isset( $custom[ $key ] ) ) {
		return false;
	} elseif ( 1 === count( $custom[ $key ] ) ) {
		return $custom[ $key ][0];
	} else {
		return $custom[ $key ];
	}
}


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.