Support

Account

Home Forums Add-ons Flexible Content Field Using acf/update_value in Flexible Content Field Reply To: Using acf/update_value in Flexible Content Field

  • The first think you need to do us use the field key variant of the acf/update_value/ hook. The reason is that sub field, field names will be different.

    The second part will be the hard part. You need to get the field from the same row of the flex field as the field that you are filtering…. rather than try to explain

    
    add_filter('acf/update_value/key=field_604150e4d40c3', 'your_function_name', 20, 4);
    function your_function_name($value, $post_id, $field, $original) {
      $name = $field['name'];
      // $name will equal {FLEX FIELD NAME}_{INDEX}_{SUB FIELD NAME}
      // we need the the flex name and index
      // stip off this sub field name
      $name = str_replace('_this_field_name', '', $name);
      // append the other sub field name
      $name .= '_other_field_name';
      // use get_post_meta to get the value from the same row
      $other_value = get_post_meta($post_id, $name, true);
      
      // set value based on other value and return
      return $value;
    }
    

    Also, I noticed that you are using a priority of 10, you should use a priority > 10 to ensure this happens after acf has saved the other value as well as making sure that the other field appears before this field when editing the page. Otherwise you may be getting the old value, or no value at all on a new post.