get_field_objects()ACF 3.6

This function will return an array containing all the custom field objects for a specific post_id. The function is not very elegant and wastes a lot of PHP memory / SQL queries if you are not using all the fields / values.

Хуков нет.

Возвращает

Массив|false. Associative array where field name => field, or false on failure.

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

get_field_objects( $post_id, $format_value, $load_value, $escape_html );
$post_id(разное)
The post_id of which the value is saved against.
По умолчанию: false
$format_value(true|false)
Whether or not to format the field value.
По умолчанию: true
$load_value(true|false)
Whether or not to load the field value.
По умолчанию: true
$escape_html(true|false)
Should the field return a HTML safe formatted value if $format_value is true.
По умолчанию: false

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

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

Код get_field_objects() ACF 6.4.2

function get_field_objects( $post_id = false, $format_value = true, $load_value = true, $escape_html = false ) {

	// init
	acf_init();

	// validate post_id
	$post_id = acf_get_valid_post_id( $post_id );

	// get meta
	$meta = acf_get_meta( $post_id );

	// bail early if no meta
	if ( empty( $meta ) ) {
		return false;
	}

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

	// populate vars
	$fields = array();
	foreach ( $meta as $key => $value ) {

		// bail if reference key does not exist
		if ( ! isset( $meta[ "_$key" ] ) || ( ! is_string( $meta[ "_$key" ] ) && ! is_numeric( $meta[ "_$key" ] ) ) ) {
			continue;
		}

		// get field
		$field = acf_get_field( $meta[ "_$key" ] );

		// bail early if no field, or if the field's name is different to $key
		// - solves problem where sub fields (and clone fields) are incorrectly allowed
		if ( ! $field || $field['name'] !== $key ) {
			continue;
		}

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

		// avoid returning field values when the function is called incorrectly.
		if ( ! $format_value && $escape_html ) {
			$field['value'] = false;
		}

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

		// append to $value
		$fields[ $key ] = $field;
	}

	// no value
	if ( empty( $fields ) ) {
		return false;
	}

	// return
	return $fields;
}