Description

Returns the value of the specified field.

This is a very versatile function. You can use it to store a value, echo a value and interact with a value. Please note the type of variable returned is relative to the field type. Eg: A repeater will return a multidimensional array.

Parameters

<?php $field = get_field($field_name, $post_id, $format_value); ?>
  • $field_name: the name of the field to be retrieved. eg “page_content” (required)
  • $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
  • $format_value: whether or not to format the value loaded from the db. Defaults to false (not required).

Usage

<?php 
 
/*
*  get a field value from the current post
*/
 
$value = get_field( "text_field" ); 
 
/*
*  get a field value from another post
*/
 
$value = get_field( "text_field", 123 ); 
 
/*
*  use get_field with conditional statements
*/
 
$value = get_field( "text_field" );
if( $value )
{
 
}
else
{
 
}
/*
*  $post_id examples
*/
 
$post_id = null; // current post
$post_id = 1;
$post_id = "option";
$post_id = "options"; // same as above
$post_id = "category_2"; // save to a specific category
$post_id = "event_3"; // save to a specific taxonomy (this tax is called "event")
$post_id = "user_1"; // save to user (user id = 1)
 
/*
*  get a value without formating
*
*  In this example, the field 'image' is an image field which would normaly return an Image object. 
*  However, by passing false as a 3rd parameter to the get_field function, the value is never formated. 
*  Therefore the value returned will be in the same format as the value saved: image ID
*
*  Note: the second parameter is false so that ACF loads from the current post
*/
 
$image = get_field('image', false, false);
?>

Related Documents