get_fields()

Description

Returns an array of custom field values for a specific page / post.

This is a very simple function which finds ALL custom field values (that don’t start with an “_”) and returns them in an associated array where the $field_name => $value

You should only use this function when you do not know the fields which which will be appearing on a template or if you have a large amount of fields which you would prefer not to code independently.

Parameters

<?php $field = get_fields($post_id); ?>
  • $post_id: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc

Usage

<?php

/*
*  get all custom fields and dump for testing
*/

$fields = get_fields();
var_dump( $fields ); 

/*
*  get all custom fields, loop through them and load the field object to create a label => value markup
*/

$fields = get_fields();

if( $fields )
{
	foreach( $fields as $field_name => $value )
	{
		// get_field_object( $field_name, $post_id, $options )
		// - $value has already been loaded for us, no point to load it again in the get_field_object function
		$field = get_field_object($field_name, false, array('load_value' => false));

		echo '<div>';
			echo '<h3>' . $field['label'] . '</h3>';
			echo $value;
		echo '</div>';
	}
}

?>