get_field_objects

Get_field_objects().

Description

get_field_objects( (constant) $post_id = false, (array) $options = array() ); 

This function will return an array containing all the custom field objects for a specific post_id. The function is not very elegant and wastes a lot of PHP memory / SQL queries if you are not using all the fields / values.

Parameters (2)

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

Usage

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

Defined (1)

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

/core/api.php  
  1. function get_field_objects( $post_id = false, $options = array() ) { 
  2.  
  3. // global 
  4. global $wpdb; 
  5.  
  6.  
  7. // filter post_id 
  8. $post_id = apply_filters('acf/get_post_id', $post_id ); 
  9.  
  10.  
  11. // vars 
  12. $field_key = ''; 
  13. $value = array(); 
  14.  
  15.  
  16. // get field_names 
  17. if( is_numeric($post_id) ) 
  18. $keys = $wpdb->get_col($wpdb->prepare( 
  19. "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = %d and meta_key LIKE %s AND meta_value LIKE %s",  
  20. $post_id,  
  21. '_%',  
  22. 'field_%' 
  23. )); 
  24. elseif( strpos($post_id, 'user_') !== false ) 
  25. $user_id = str_replace('user_', '', $post_id); 
  26.  
  27. $keys = $wpdb->get_col($wpdb->prepare( 
  28. "SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d and meta_key LIKE %s AND meta_value LIKE %s",  
  29. $user_id,  
  30. '_%',  
  31. 'field_%' 
  32. )); 
  33. else 
  34. $keys = $wpdb->get_col($wpdb->prepare( 
  35. "SELECT option_value FROM $wpdb->options WHERE option_name LIKE %s",  
  36. '_' . $post_id . '_%'  
  37. )); 
  38.  
  39.  
  40. if( is_array($keys) ) 
  41. foreach( $keys as $key ) 
  42. $field = get_field_object( $key, $post_id, $options ); 
  43.  
  44. if( !is_array($field) ) 
  45. continue; 
  46.  
  47. $value[ $field['name'] ] = $field; 
  48.  
  49.  
  50. // no value 
  51. if( empty($value) ) 
  52. return false; 
  53.  
  54.  
  55. // return 
  56. return $value;