get_post_field() WordPress Function

The get_post_field() function allows you to get a specific field from a post. The first parameter is the field you want to get, and the second parameter is the post ID.

get_post_field( string $field, int|WP_Post $post = null, string $context = 'display' ) #

Retrieve data from a post field based on Post ID.


Description

Examples of the post field will be, ‘post_type’, ‘post_status’, ‘post_content’, etc and based off of the post object property or key names.

The context values are based off of the taxonomy filter functions and supported values are found within those functions.

Top ↑

See also


Top ↑

Parameters

$field

(string)(Required)Post field name.

$post

(int|WP_Post)(Optional) Post ID or post object. Defaults to global $post.

Default value: null

$context

(string)(Optional) How to filter the field. Accepts 'raw', 'edit', 'db', or 'display'.

Default value: 'display'


Top ↑

Return

(string) The value of the post field on success, empty string on failure.


Top ↑

Source

File: wp-includes/post.php

function get_post_field( $field, $post = null, $context = 'display' ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return '';
	}

	if ( ! isset( $post->$field ) ) {
		return '';
	}

	return sanitize_post_field( $field, $post->$field, $post->ID, $context );
}


Top ↑

Changelog

Changelog
VersionDescription
4.5.0The $post parameter was made optional.
2.3.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