get_field_object

Get_field_object().

Description

get_field_object( $field_key, (constant) $post_id = false, (array) $options = array() ); 

This function will return an array containing all the field data for a given field_name

Parameters (3)

0. $field_key
The field key.
1. $post_id — Optional. (constant) => false
The post id.
2. $options — Optional. (array) => array()
The options.

Usage

  1. if ( !function_exists( 'get_field_object' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'advanced-custom-fields/core/api.php'; 
  3.  
  4. // The field key. 
  5. $field_key = null; 
  6.  
  7. // The post id. 
  8. $post_id = false; 
  9.  
  10. // The options. 
  11. $options = array(); 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = get_field_object($field_key, $post_id, $options); 
  15.  

Defined (1)

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

/core/api.php  
  1. function get_field_object( $field_key, $post_id = false, $options = array() ) { 
  2.  
  3. // make sure add-ons are included 
  4. acf()->include_3rd_party(); 
  5.  
  6.  
  7. // filter post_id 
  8. $post_id = apply_filters('acf/get_post_id', $post_id ); 
  9. $field = false; 
  10. $orig_field_key = $field_key; 
  11.  
  12.  
  13. // defaults for options 
  14. $defaults = array( 
  15. 'load_value' => true,  
  16. 'format_value' => true,  
  17. ); 
  18.  
  19. $options = array_merge($defaults, $options); 
  20.  
  21.  
  22. // is $field_name a name? pre 3.4.0 
  23. if( substr($field_key, 0, 6) !== 'field_' ) 
  24. // get field key 
  25. $field_key = get_field_reference( $field_key, $post_id ); 
  26.  
  27.  
  28. // get field 
  29. if( substr($field_key, 0, 6) === 'field_' ) 
  30. $field = apply_filters('acf/load_field', false, $field_key ); 
  31.  
  32.  
  33. // validate field 
  34. if( !$field ) 
  35. // treat as text field 
  36. $field = array( 
  37. 'type' => 'text',  
  38. 'name' => $orig_field_key,  
  39. 'key' => 'field_' . $orig_field_key,  
  40. ); 
  41. $field = apply_filters('acf/load_field', $field, $field['key'] ); 
  42.  
  43.  
  44. // load value 
  45. if( $options['load_value'] ) 
  46. $field['value'] = apply_filters('acf/load_value', false, $post_id, $field); 
  47.  
  48.  
  49. // format value 
  50. if( $options['format_value'] ) 
  51. $field['value'] = apply_filters('acf/format_value_for_api', $field['value'], $post_id, $field); 
  52.  
  53.  
  54. return $field; 
  55.