Outil de recherche WordPress pour les développeurs et auteurs de thèmes



get_post_field ›

Depuis2.3.0
Dépréciéen/a
get_post_field ( $field, $post = null, $context = 'display' )
Paramètres: (3)
  • (string) $field Post field name.
    Requis: Oui
  • (int|WP_Post) $post Optional. Post ID or post object. Defaults to global $post.
    Requis: Non
    Défaut: null
  • (string) $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db', or 'display'. Default 'display'.
    Requis: Non
    Défaut: 'display'
Voir:
Retourne:
  • (string) The value of the post field on success, empty string on failure.
Défini(e) dans:
Codex:
Changelog:
  • 4.5.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.



Source

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 );
}