get_field_object()

Overview

This function will return an array of data containing all field settings. The field’s value will also be loaded unless otherwise specified.

Parameters

get_field_object($selector, $post_id, $format_value, $load_value)
  • $field_name (required) the field’s name or key
  • $post_id specific post ID where a value was entered. Defaults to current post ID
  • $format_value whether or not to format the value loaded from the db. Defaults to true
  • $load_value whether or not to load the value loaded from the db. Defaults to true

Return

This function will return an array looking something like the following. Please note that each field contains unique settings.

array(
	'ID'				=> 0,
	'key'				=> '',
	'label'				=> '',
	'name'				=> '',
	'prefix'			=> '',
	'type'				=> 'text',
	'value'				=> null,
	'menu_order'		=> 0,
	'instructions'		=> '',
	'required'			=> 0,
	'id'				=> '',
	'class'				=> '',
	'conditional_logic'	=> 0,
	'parent'			=> 0,
	'wrapper'			=> array(
		'width'				=> '',
		'class'				=> '',
		'id'				=> ''
	)
);

Usage

<?php 

/*
*  Get a field object and display it with it's value
*/

$field_name = "text_field";
$field = get_field_object($field_name);

echo $field['label'] . ': ' . $field['value'];

/*
*  Get a field object and display it with it's value (using the field key and the value fron another post)
*/

$field_key = "field_5039a99716d1d";
$post_id = 123;
$field = get_field_object($field_key, $post_id);

echo $field['label'] . ': ' . $field['value'];

/*
*  Get a field object and create a select form element
*/

$field_key = "field_5039a99716d1d";
$field = get_field_object($field_key);

if( $field )
{
	echo '<select name="' . $field['key'] . '">';
		foreach( $field['choices'] as $k => $v )
		{
			echo '<option value="' . $k . '">' . $v . '</option>';
		}
	echo '</select>';
}

?>