get_field_object()ACF 3.6

This function will return an array containing all the field data for a given field_name.

No Hooks.

Returns

Array|false. $field

Usage

get_field_object( $selector, $post_id, $format_value, $load_value, $escape_html );
$selector(string) (required)
The field name or key.
$post_id(mixed)
The post_id of which the value is saved against.
Default: false
$format_value(true|false)
Whether to format the field value.
Default: true
$load_value(true|false)
Whether to load the field value.
Default: true
$escape_html(true|false)
Should the field return a HTML safe formatted value if $format_value is true.
Default: false

Changelog

Since 3.6 Introduced.

get_field_object() code ACF 6.8.8

function get_field_object( $selector, $post_id = false, $format_value = true, $load_value = true, $escape_html = false ) {
	// Compatibility with ACF ~4.
	if ( is_array( $format_value ) && isset( $format_value['format_value'] ) ) {
		$format_value = $format_value['format_value'];
	}

	$post_id = acf_get_valid_post_id( $post_id );
	$field   = acf_maybe_get_field( $selector, $post_id );

	if ( ! $field ) {
		return false;
	}

	if ( $load_value ) {
		$field['value'] = acf_get_value( $post_id, $field );
	}

	// escape html is only compatible when formatting the value too
	if ( ! $format_value && $escape_html ) {
		_doing_it_wrong( __FUNCTION__, __( 'Returning an escaped HTML value is only possible when format_value is also true. The field value has not been returned for security.', 'acf' ), '6.2.6' ); //phpcs:ignore -- escape not required.
		$field['value'] = false;
		return $field;
	}

	// format value
	if ( $load_value && $format_value ) {
		if ( $escape_html ) {
			// return the escaped HTML version if requested.
			if ( acf_field_type_supports( $field['type'], 'escaping_html' ) ) {
				$field['value'] = acf_format_value( $field['value'], $post_id, $field, true );
			} else {
				$new_value = acf_format_value( $field['value'], $post_id, $field );
				if ( is_array( $new_value ) ) {
					$field['value'] = map_deep( $new_value, 'acf_esc_html' );
				} else {
					$field['value'] = acf_esc_html( $new_value );
				}
			}
		} else {
			// get value for field
			$field['value'] = acf_format_value( $field['value'], $post_id, $field );
		}
	}

	return $field;
}