Support

Account

Home Forums Bug Reports "update_field" not working if called inside "acf/update_value" filter

Solved

"update_field" not working if called inside "acf/update_value" filter

  • Howdy,

    I have a filter that looks like this:

    add_filter('acf/update_value/name=google_maps', function ($value, $post_id, $field, $original) {
      update_field('another_field', 'foo', $post_id);
      return $value;
    });

    update_field does not work, though. For it to work, I had to call it from outside the filter:

    add_filter('acf/update_value/name=google_maps', function ($value, $post_id, $field, $original) {
      global $updateAnotherFieldId;
      global $updateAnotherFieldValue;
      $updateAnotherFieldId = $post_id;
      $updateAnotherFieldValue = 'foo';
      add_action('shutdown', 'update_another_field');
    
      return $value;
    });
    
    function update_another_field() {
      global $updateAnotherFieldId;
      global $updateAnotherFieldValue;
      update_field('another_field', $updateAnotherFieldValue, $updateAnotherFieldId);
    }

    Is there a reason for why update_field would not work inside an acf/update_value filter?

    This is a single-site installation.

  • acf/update_value happens as each field is updated. Fields are updated in the order they are submitted, which is the same order that they appear in the HTML. If you attempt to update a field that is processed after the field you add the filter to then ACF will update that field with the submitted value overwriting what you updated the field to.

    In this case you would be better off using an acf/save_post filter with a priority > 10 and do your updated then.

  • @hube2 Brilliant! That was exactly the reason, thanks for the explanation.

    This is very hard to figure out by yourself, though, I will open a ticket suggesting a doing_it_wrong if calling update_field while in the context of a acf/update_value filter

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

You must be logged in to reply to this topic.