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

Retrieve data from a post field based on Post ID.


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

See also See also


Top ↑

Parameters Parameters

$field

(string) (Required) Post field name.

$post

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

Default value: null

$context

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

Default value: 'display'


Top ↑

Return Return

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


Top ↑

Source 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

Changelog
Version Description
4.5.0 The $post parameter was made optional.
2.3.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 2 content
    Contributed by Andrei Surdu

    Here are the default post fields that you can get (case-sensitive):

    ID
    post_author
    post_date
    post_date_gmt
    post_content
    post_title
    post_excerpt
    post_status
    comment_status
    ping_status
    post_password
    post_name
    to_ping
    pinged
    post_modified
    post_modified_gmt
    post_content_filtered
    post_parent
    guid
    menu_order
    post_type
    post_mime_type
    comment_count
    filter
    

You must log in before being able to contribute a note or feedback.