Support

Account

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

Solved

Add repeater row on post save, only if other field is updated

  • Hello,

    I’m struggling to figure out how to accomplish adding a repeater row to a post ONLY IF another ACF field’s value changed.

    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.

    If I use the acf/save_post action with a priority >= 10 (after the post is saved), there doesn’t seem to be a way to compare if the field value’s changed, but I can get a new value to store via update_field().

    I’ve also tried using acf/update_value, but I’m running into the same priority and comparison issues…

    Any ideas? Thanks!

  • Hi @scferg

    You need to use:

    $row = array(
        'image' => 123,
        'alt'   => 'Another great sunset',
        'link'  => 'http://website.com'
    );
    
    add_row('images', $row);

    That adds the row rather than using update_field()

  • Hi @jarvis

    Thanks for the info. Which action and priority would you recommend using with add_row() while being able to compare new and old values from the other ACF field?

    Also, how would this scenario work with updating an ACF field that’s not a repeater? Using update_field() didn’t seem to work given the comparisons needed and order of operations in my original post.

    Thank you!

  • HI @scferg

    I would use the acf/save_post filter, according to the docs:

    This action allows you to hook in before or after the $_POST data has been saved, making it useful to perform additional functionality when updating a post or other WP object.

    If you’re trying to update a non-repeater field, then use update_field();

    You could also look at acf/update_value I wonder if this may help more with the comparison side of things.

  • 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;
    }
    
  • Hi @hube2,

    Thanks for the explanation! That did the trick!

    In reading documentation regarding modifying the $_POST data, I wasn’t sure how that would interface with repeater rows as the keys seemed to be named sequentially. Good to know I can modify repeater data this way.

    Thanks so much!

    Thank you, @jarvis as well. 🙂

  • The row index that is submitted is ignored when ACF is updating. It is only needed so that the browser can group the rows correctly in the data.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.