4. get_field_object() -- Field Definitions | Field Forge - Custom Fields, Built for Speed
Download Log in

4. get_field_object() — Field Definitions

Developer Guide

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:
ParameterTypeDefaultDescription
$selectorstringField name or field key
$post_idintstringfalsefalsePost ID, 'options', or false for current post
Returns: array|false — field definition array with a value key, or false if not found. Returned array keys:
KeyTypeDescription
keystringField key (e.g., 'field_abc123def')
labelstringHuman-readable label
namestringField name (slug)
typestringField type (e.g., 'select', 'text')
choicesarrayChoices for select/checkbox/radio fields
return_formatstringHow the value is formatted
requiredboolWhether the field is required
valuemixedCurrent value for this post
_group_idintParent 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'

Forge AI Assistant Online

Hi! I'm the Field Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs