get_field_objects()

Last updated Feb 6, 2024
Link to heading

Description

Returns the settings of all fields saved on a specific post.

Each field contains many settings such as a label, name and type. This function can be used to load these settings as an array along with the field’s value.

Link to heading

Parameters

get_field_objects([$post_id = false], [$format_value = true], [$load_value = true], [$escape_html = false]);
  • $post_id (mixed) (Optional) The post ID where the value is saved. Defaults to false, which uses the current post.
  • $format_value (bool) (Optional) Whether to apply formatting logic. Defaults to true.
  • $load_value (bool) (Optional) Whether to load the field’s value. Defaults to true.
  • $escape_html (bool) (Optional) Since 6.2.6 – return an escaped HTML safe version of the field value. Requires $format_value to be true. Defaults to false.
Link to heading

Return

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

array(
    "my_field" => 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'                => ''
        )
    ),
    ...
);
Link to heading

Examples

Link to heading

Display all fields labels and values

This example shows how to load all fields and display their labels and values.

<?php
$fields = get_field_objects();
if( $fields ): ?>
    <ul>
        <?php foreach( $fields as $field ): ?>
            <li><?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>
Link to heading

Display all fields labels and values from a specific post

This example shows how to load all fields and display their labels and values from the post with ID = 123.

<?php
$fields = get_field_objects( 123 );
if( $fields ): ?>
    <ul>
        <?php foreach( $fields as $field ): ?>
            <li><?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>
Supercharge Your Website With Premium Features Using ACF PRO

Speed up your workflow and unlock features to better develop websites using ACF Blocks and Options Pages, with the Flexible Content, Repeater, Clone, Gallery Fields & More.

Explore Features View Pricing

PRO Features
ACF Blocks
Options Pages
PRO Fields
Repeater
Flexible Content
Gallery
Clone
Link to heading

Related

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.