get_post_field

Retrieve data from a post field based on Post ID.

Description

(string) get_post_field( (string) $field, (constant) $post = null, (string) $context = 'display' ); 

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.

Returns (string)

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

Parameters (3)

0. $field (string)
Post field name.
1. $post — Optional. (constant) => null
Post ID or post object. Defaults to current post.
2. $context — Optional. (string) => 'display'
How to filter the field. Accepts raw,, edit., db, or display. Default display.

Usage

  1. if ( !function_exists( 'get_post_field' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Post field name. 
  5. $field = ''; 
  6.  
  7. // Optional. Post ID or post object. Defaults to current post. 
  8. $post = null; 
  9.  
  10. // Optional. How to filter the field. Accepts 'raw', 'edit', 'db', 
  11. // or 'display'. Default 'display'. 
  12. $context = 'display'; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = get_post_field($field, $post, $context); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post.php  
  1. function get_post_field( $field, $post = null, $context = 'display' ) { 
  2. $post = get_post( $post ); 
  3.  
  4. if ( !$post ) 
  5. return ''; 
  6.  
  7. if ( !isset($post->$field) ) 
  8. return ''; 
  9.  
  10. return sanitize_post_field($field, $post->$field, $post->ID, $context);