get_fields()ACF 3.6

Returns an array containing all custom-field values for a specific post, user, comment, term, and so on.

The function is not very elegant and uses a lot of PHP memory / SQL queries if you do not use all values.

No Hooks.

Returns

Array|false. An associative array (field name => field value).

Usage

get_fields( $post_id, $format_value );
$post_id(mixed)
The ID of the post (user, category, etc.) for which to get meta-fields.
Default: false (the current post ID in the loop)
$format_value(bool)
Apply the formatting specified in the field settings.
Default: true

Examples

#1 Get meta-field values for the current post

Display all meta-fields (field names and their values) for the current post as a list.

$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; ?>

#2 Get values for different entities

// Get all meta-field values for the current post.
$fields = get_fields();

// Get all meta-field values for the post with ID = 1.
$post_fields = get_fields( 1 );

// Get all meta-field values for the user with ID = 2.
$user_fields = get_fields( 'user_2' );

// Get all meta-field values for the term with ID = 3.
$term_fields = get_fields( 'term_3' );

// Get all meta-field values for the category term with ID = 3.
$term_fields = get_fields( 'category_3' );

// Get all meta-field values for the comment with ID = 4.
$comment_fields = get_fields( 'comment_4' );

// Get all meta-field values for all options pages created by ACF.
$option_fields = get_fields( 'options' );

// Or this way.
$option_fields = get_fields( 'option' );

#3 Get meta-field values for the current post without formatting

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

Formatting concerns how values are changed after being loaded from the database. For example, an image field value is stored in the database as an attachment ID only, but it can be returned as a URL depending on the field settings.

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

$fields = get_fields( 123, false );

Changelog

Since 3.6 Introduced.

get_fields() code ACF 6.8.8

function get_fields( $post_id = false, $format_value = true, $escape_html = false ) {

	// escape html is only compatible when formatting the value too
	if ( ! $format_value && $escape_html ) {
		_doing_it_wrong( __FUNCTION__, __( 'Returning escaped HTML values is only possible when format_value is also true. The field values have not been returned for security.', 'acf' ), '6.2.6' ); //phpcs:ignore -- escape not required.
		return false;
	}

	// vars
	$fields = get_field_objects( $post_id, $format_value, true, $escape_html );
	$meta   = array();

	// bail early
	if ( ! $fields ) {
		return false;
	}

	// populate
	foreach ( $fields as $k => $field ) {
		$meta[ $k ] = $field['value'];
	}

	// return
	return $meta;
}