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.

Хуков нет.

Возвращает

(Массив). associative array where field name => field

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

get_field_objects( $post_id, $format_value, $load_value );
$post_id **
-
По умолчанию: false
$format_value **
-
По умолчанию: true
$load_value **
-
По умолчанию: true

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

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

Код get_field_objects() ACF 6.0.4

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

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

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

		// format value
		if ( $format_value ) {
			$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;
}