update_field

Update_field().

Description

update_field( $field_key, $value, (bool) $post_id = false ); 

This function will update a value in the database

Parameters (3)

0. $field_key
The field key.
1. $value
The value.
2. $post_id — Optional. (bool) => false
The post id.

Usage

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

Defined (1)

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

/core/api.php  
  1. function update_field( $field_key, $value, $post_id = false ) 
  2. // filter post_id 
  3. $post_id = apply_filters('acf/get_post_id', $post_id ); 
  4.  
  5.  
  6. // vars 
  7. $options = array( 
  8. 'load_value' => false,  
  9. 'format_value' => false 
  10. ); 
  11.  
  12. $field = get_field_object( $field_key, $post_id, $options); 
  13.  
  14.  
  15. // sub fields? They need formatted data 
  16. if( $field['type'] == 'repeater' ) 
  17. $value = acf_convert_field_names_to_keys( $value, $field ); 
  18. elseif( $field['type'] == 'flexible_content' ) 
  19. if( $field['layouts'] ) 
  20. foreach( $field['layouts'] as $layout ) 
  21. $value = acf_convert_field_names_to_keys( $value, $layout ); 
  22.  
  23.  
  24. // save 
  25. do_action('acf/update_value', $value, $post_id, $field ); 
  26.  
  27.  
  28. return true; 
  29.