acf_get_reference()ACF 5.6.5

acf_get_reference

Retrieves the field key for a given field name and post_id.

Возвращает

Строку|null. The field key, or null on failure.

Использование

acf_get_reference( $field_name, $post_id );
$field_name(строка) (обязательный)
The name of the field. eg 'sub_heading'.
$post_id(разное) (обязательный)
The post_id of which the value is saved against.

Список изменений

С версии 5.6.5 Введена.

Код acf_get_reference() ACF 6.4.2

function acf_get_reference( $field_name, $post_id ) {
	// Allow filter to short-circuit load_value logic.
	$reference = apply_filters( 'acf/pre_load_reference', null, $field_name, $post_id );
	if ( $reference !== null ) {
		return $reference;
	}

	// Back-compat.
	$reference = apply_filters( 'acf/pre_load_metadata', null, $post_id, $field_name, true );
	if ( $reference !== null ) {
		return ( $reference === '__return_null' ) ? null : $reference;
	}

	$decoded = acf_decode_post_id( $post_id );

	// Bail if no ID or type.
	if ( empty( $decoded['id'] ) || empty( $decoded['type'] ) ) {
		return null;
	}

	$meta_instance = acf_get_meta_instance( $decoded['type'] );
	if ( ! $meta_instance ) {
		return null;
	}

	// Get hidden meta for this field name.
	$reference = $meta_instance->get_reference( $decoded['id'], $field_name );

	/**
	 * Filters the reference value.
	 *
	 * @date    25/1/19
	 * @since   5.7.11
	 *
	 * @param   string $reference The reference value.
	 * @param   string $field_name The field name.
	 * @param   (int|string) $post_id The post ID where meta is stored.
	 */
	return apply_filters( 'acf/load_reference', $reference, $field_name, $post_id );
}