Get the full field definition (type, label, choices, etc.) along with its current value for a single field.
get_field_object( $selector, $post_id )
Parameters:
| Parameter | Type | Default | Description | ||
|---|---|---|---|---|---|
$selector | string | — | Field name or field key | ||
$post_id | int | string | false | false | Post ID, 'options', or false for current post |
array|false — field definition array with a value key, or false if not found.
Returned array keys:
| Key | Type | Description |
|---|---|---|
key | string | Field key (e.g., 'field_abc123def') |
label | string | Human-readable label |
name | string | Field name (slug) |
type | string | Field type (e.g., 'select', 'text') |
choices | array | Choices for select/checkbox/radio fields |
return_format | string | How the value is formatted |
required | bool | Whether the field is required |
value | mixed | Current value for this post |
_group_id | int | Parent field group ID |
php
$field = get_field_object( 'product_color' );
/*
Returns:
[
'key' => 'field_abc123def',
'label' => 'Product Color',
'name' => 'product_color',
'type' => 'select',
'choices' => ['red' => 'Red', 'blue' => 'Blue', 'green' => 'Green'],
'return_format' => 'value',
'required' => true,
'value' => 'blue',
'_group_id' => 3,
]
*/
// Render a select field dynamically based on its definition
$field = get_field_object( 'status' );
if ( $field ) {
echo '<label>' . esc_html( $field['label'] ) . '</label>';
echo '<select name="status">';
foreach ( $field['choices'] as $value => $label ) {
$selected = ( $field['value'] === $value ) ? ' selected' : '';
echo '<option value="' . esc_attr( $value ) . '"' . $selected . '>' . esc_html( $label ) . '</option>';
}
echo '</select>';
}
// Build a front-end filter UI from field definitions
$field = get_field_object( 'product_category' );
if ( $field && ! empty( $field['choices'] ) ) {
echo '<div class="filter-group">';
echo '<h4>' . esc_html( $field['label'] ) . '</h4>';
foreach ( $field['choices'] as $val => $lbl ) {
printf(
'<label><input type="checkbox" name="filter_%s[]" value="%s"> %s</label>',
esc_attr( $field['name'] ),
esc_attr( $val ),
esc_html( $lbl )
);
}
echo '</div>';
}
// Get field definition from options page
$logo_field = get_field_object( 'site_logo', 'options' );
echo 'Logo field type: ' . $logo_field['type']; // 'image'—