Support

Account

Home Forums General Issues Add repeater row on post save, only if other field is updated Reply To: Add repeater row on post save, only if other field is updated

  • In the scenario what you have outlined, that is to add a row to a repeater only if some other field has changed. This depends on if the repeater is being submitted when the other field is submitted.

    Your comment

    If I use the acf/save_post action with a priority < 10 (before the post is saved), I can compare the field’s value in $_POST compared to what’s currently in the DB for the post via get_field(). However, if I try to use update_field() to add the repeater row, it gets overwritten when $_POST is actually saved — so the new row gets cleared out.

    tells me that this is the case.

    For this I would use an acf/save_post filter with a priority of < 10 so that I can compare the new/old values. In this filter I would modify $_POST[‘acf’] with the added row so that ACF can then update the repeater.

    
    // example contents of acf/save_post action
    // check to see if the value is change
    if ($_POST['acf']['field_XYZ123'] != get_field('field_name', $post_id)) {
      // build new repeater row
      $row = array(
        // the row requires sub field key => value pairs
        'field_123ACB' => 'new sub field value for this row';
      );
      // add this row to the submitted repeater
      $_POST['acf']['field_ZYX321'][] = $row;
    }