get_fields()

Last updated Feb 6, 2024

Description

Returns an array of field values (name => value) for a specific post.

Parameters

get_fields([$post_id = false], [$format_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.
  • $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.

Return

(array|false) Array of values or false if no fields are found.

Examples

Get values from the current post

This example shows how to display all fields (name and value) in a list from the current post.

<?php 

$fields = get_fields();

if( $fields ): ?>
    <ul>
        <?php foreach( $fields as $name => $value ): ?>
            <li><b><?php echo $name; ?></b> <?php echo $value; ?></li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Get values from a specific post

This example shows how to load all fields (name and value) from different data objects.

// Get values from the current post.
$fields = get_fields();

// Get values from post ID = 1.
$post_fields = get_fields( 1 );

// Get values from user ID = 2.
$user_fields = get_fields( 'user_2' );

// Get values from category ID = 3.
$term_fields = get_fields( 'term_3' );

// ... or using taxonomy name.
$term_fields = get_fields( 'category_3' );

// Get values from comment ID = 4.
$comment_fields = get_fields( 'comment_4' );

// Get values from ACF Options page.
$option_fields = get_fields( 'options' );

// ... or using 'option'.
$option_fields = get_fields( 'option' );

Get values without formatting

This example shows how to load all fields (name and value) without any formatting applied.

Formatting refers to how the values are modified after being loaded from the database. For example, an image field value is saved into the database as just the attachment ID but can be returned as a URL depending on the field’s settings.

In some cases it may be useful to ensure only the raw value is returned regardless of field settings. To do this, we use the $format_value parameter.

$fields = get_fields( 123, false );
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

Related