get_post_field

The timeline below displays how wordpress function get_post_field has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Retrieves data from a post field based on Post ID.
 *
 * 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.
 *
 * @since 2.3.0
 * @since 4.5.0 The `$post` parameter was made optional.
 *
 * @see sanitize_post_field()
 *
 * @param string      $field   Post field name.
 * @param int|WP_Post $post    Optional. Post ID or post object. Defaults to global $post.
 * @param string      $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db',
 *                             or 'display'. Default 'display'.
 * @return string The value of the post field on success, empty string on failure.
 */
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);
}

WordPress Version: 5.1

/**
 * Retrieve data from a post field based on Post ID.
 *
 * 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.
 *
 * @since 2.3.0
 * @since 4.5.0 The `$post` parameter was made optional.
 *
 * @see sanitize_post_field()
 *
 * @param string      $field   Post field name.
 * @param int|WP_Post $post    Optional. Post ID or post object. Defaults to global $post.
 * @param string      $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db',
 *                             or 'display'. Default 'display'.
 * @return string The value of the post field on success, empty string on failure.
 */
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);
}

WordPress Version: 4.5

/**
 * Retrieve data from a post field based on Post ID.
 *
 * 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.
 *
 * @since 2.3.0
 * @since 4.5.0 The `$post` parameter was made optional.
 *
 * @see sanitize_post_field()
 *
 * @param string      $field   Post field name.
 * @param int|WP_Post $post    Optional. Post ID or post object. Defaults to current post.
 * @param string      $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db',
 *                             or 'display'. Default 'display'.
 * @return string The value of the post field on success, empty string on failure.
 */
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);
}

WordPress Version: 4.0

/**
 * Retrieve data from a post field based on Post ID.
 *
 * 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.
 *
 * @since 2.3.0
 *
 * @see sanitize_post_field()
 *
 * @param string      $field   Post field name.
 * @param int|WP_Post $post    Post ID or post object.
 * @param string      $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db',
 *                             or 'display'. Default 'display'.
 * @return string The value of the post field on success, empty string on failure.
 */
function get_post_field($field, $post, $context = 'display')
{
    $post = get_post($post);
    if (!$post) {
        return '';
    }
    if (!isset($post->{$field})) {
        return '';
    }
    return sanitize_post_field($field, $post->{$field}, $post->ID, $context);
}

WordPress Version: 3.9

/**
 * Retrieve data from a post field based on Post ID.
 *
 * 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.
 *
 * @since 2.3.0
 * @uses sanitize_post_field() See for possible $context values.
 *
 * @param string $field Post field name.
 * @param int|WP_Post $post Post ID or post object.
 * @param string $context Optional. How to filter the field. Default is 'display'.
 * @return string The value of the post field on success, empty string on failure.
 */
function get_post_field($field, $post, $context = 'display')
{
    $post = get_post($post);
    if (!$post) {
        return '';
    }
    if (!isset($post->{$field})) {
        return '';
    }
    return sanitize_post_field($field, $post->{$field}, $post->ID, $context);
}

WordPress Version: 3.7

/**
 * Retrieve data from a post field based on Post ID.
 *
 * 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.
 *
 * @since 2.3.0
 * @uses sanitize_post_field() See for possible $context values.
 *
 * @param string $field Post field name.
 * @param int|object $post Post ID or post object.
 * @param string $context Optional. How to filter the field. Default is 'display'.
 * @return string The value of the post field on success, empty string on failure.
 */
function get_post_field($field, $post, $context = 'display')
{
    $post = get_post($post);
    if (!$post) {
        return '';
    }
    if (!isset($post->{$field})) {
        return '';
    }
    return sanitize_post_field($field, $post->{$field}, $post->ID, $context);
}