Home Forums General Issues acf/save_post not working with calculated update_field Reply To: acf/save_post not working with calculated update_field

  • So, you have a couple of choices, the first is to construct an array with all of the fields that you want to check and update. This array will need to look something like this so that you have field names and keys, you will need the field keys for updating the field.

    $fields = array(
      'field_name' => 'field_key',
      'field_name' => 'field_key',
      'field_name' => 'field_key',
      'field_name' => 'field_key',
      'field_name' => 'field_key'

    then you can loop through this array and test for values, get values and update values.

    or you can do something like this, see comments about what’s going on

    /* Load Defaults from options page and user page*/
    function update_save($post_id) {
      // Get Post Type From POST ID
      $post_type = get_post_type($post_id); 
      // you should check the post type is your post type and return if not
      if ($post_type != 'your-post-type') {
      // Check to see if draft or pending
      if (get_post_status ( $post_id ) == 'draft' || get_post_status ( $post_id ) == 'pending') {
        $post_author_id = 'user_'.get_post_field( 'post_author', $post_id );
        // acf_get_fields() is an internal ACF funciton
        // that will return all fields in a field group
        // repeater fields will be nested in $field['sub_fields']
        // this is the only way to get a list of all of the fields
        // if some of them do not have values
        $group_key = 'group_57007d5b46c49';
        $fields = acf_get_fields($group_key);
        foreach ($fields as $field) {
          $replace = get_field($field['name'], $post_id);
          if (!empty($relace)) {
            // field has a value
            // go to the next field
          $default_value = get_field($field['name'], $post_author_id);
          if (empty($default_value)) {
            $default_value = get_field($field['name'], 'options');  
          update_field($field['key'], $default_value);
      } else {
    add_action('acf/save_post', 'update_save', 50); //Trigger after every save containing ACF fields

    let me know if you have any questions.