Support

Account

Home Forums Bug Reports Programmatically inserting field with update_field() does not at _field_key meta Reply To: Programmatically inserting field with update_field() does not at _field_key meta

  • As I stated before, when inserting data you must use the field keys, field names will not work where there is not already existing data saved to the field, which is what happens when you’re adding new posts.

    For top level field (not in a group, repeater, flex field)

    
    update_field('field_XYZ123', $value, $post_id)
    

    for a repeater or a flex field

    
    $value = array(
      // each element of this array is a row in the repeater
      array(
        // each element of this row is a field key => value pair
        'field_XXXXXXX' => 'value',
        'field_YYYYYYY' => 'value'
      )
    );
    // field_ZZZZZZZ is the repeater field key
    update_field('field_ZZZZZZZ', $value, $post_id);
    

    for a group field

    
    $value = array(
      // a group field is a repeater with a single row
      // there is no nesting required for each row because of this
      // the elements of this array are sub field key => value pairs
      'field_XXXXXXX' => 'value',
      'field_YYYYYYY' => 'value'
    );
    // field_ZZZZZZZ is the group field key
    update_field('field_ZZZZZZZ', $value, $post_id);
    

    If you have nested repeater, group and flex fields then you must create the nesting when updating the fields. It may be possible to use add_row() and update_sub_field(), but these would have the same restrictions of needing to use the field key for new data and it’s generally easier to just construct the arrays that are needed. But then again, I don’t generally insert data this way. If I’m inserting data it’s because I am building an import and I use WP All Import Pro with the ACF add on so that I don’t need to worry about these details.

    The comment I made about deleting the cache is only relevant if you update a field and then want to get the updated value during the same page load unless you have some type of persistent cache installed that caches data across page loads.