Получить полное определение поля (тип, метка, выбор и т.д.) вместе с его текущим значением для одного поля.
get_field_object( $selector, $post_id )
Параметры:
| Параметр | Тип | По умолчанию | Описание | ||
|---|---|---|---|---|---|
$selector | string | — | Имя поля или ключ поля | ||
$post_id | int\ | string\ | false | false | ID записи, 'options' или false для текущей записи |
array|false — массив определения поля с ключом value, или false, если не найдено.
Ключи возвращаемого массива:
| Ключ | Тип | Описание |
|---|---|---|
key | string | Ключ поля (например, 'field_abc123def') |
label | string | Читаемая метка |
name | string | Имя поля (слаг) |
type | string | Тип поля (например, 'select', 'text') |
choices | array | Выбор для полей select/checkbox/radio |
return_format | string | Как значение форматируется |
required | bool | Обязательно ли поле |
value | mixed | Текущее значение для этой записи |
_group_id | int | 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'—