get_field_objects()

Description

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

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

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

$fields = get_field_objects($post_id, [$format_value], [$load_value]);
  • $post_id (mixed) (Optional) The post ID where the value is saved. Defaults to the current post.
  • $format_value (bool) (Optional) Whether to apply formatting to the field values. Defaults to true.
  • $load_value (bool) (Optional) Whether to load the field values. Defaults to true.

Usage

<?php

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

$fields = get_field_objects();
var_dump( $fields ); 

/*
*  get all custom fields, loop through them and create a label => value markup
*/

$fields = get_field_objects();

if( $fields )
{
	foreach( $fields as $field_name => $field )
	{
		echo '<div>';
			echo '<h3>' . $field['label'] . '</h3>';
			echo $field['value'];
		echo '</div>';
	}
}

?>

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.