get_field

The advanced-custom-fields get field function.

Published Date - March 18, 2020

Description

The acf get_field function return the value of acf custom field.

  1. $selector is name or field key
  2. $post_id is the post id

Example : fiels name is message-title

echo get_field('message-title', 123);



Where can I get the ACF field name or key?

Go to wp-admin > custom fields > edit field groups.


get_field( $selector, $post_id, $format_value );

Parameters

  1. $selector

  2. $post_id

  3. $format_value


Usage

The following example is for adding a hook callback.

if ( !function_exists( 'get_field' ) ) {
    require_once ABSPATH . PLUGINDIR . 'advanced-custom-fieldsadvanced-custom-fields/includes/api/api-template.php';
}

// The input(s). 
$selector = null;
$post_id = false;
$format_value = true;

// NOTICE! Understand what this does before running.
$result = get_field( $selector, $post_id, $format_value );
            

Defined

The function is defined in the following location(s).

advanced-custom-fields/includes/api/api-template.php

function get_field( $selector, $post_id = false, $format_value = true ) {
	
	// filter post_id
	$post_id = acf_get_valid_post_id( $post_id );
	
	
	// get field
	$field = acf_maybe_get_field( $selector, $post_id );
	
	
	// create dummy field
	if( !$field ) {
		
		$field = acf_get_valid_field(array(
			'name'	=> $selector,
			'key'	=> '',
			'type'	=> '',
		));
		
		
		// prevent formatting
		$format_value = false;
		
	}
	
	
	// get value for field
	$value = acf_get_value( $post_id, $field );
	
	
	// format value
	if( $format_value ) {
		
		// get value for field
		$value = acf_format_value( $value, $post_id, $field );
		
	}
	
	
	// return
	return $value;
	 
}