Support

Account

Home Forums Front-end Issues get_field in field filter cause infinite loop

Solved

get_field in field filter cause infinite loop

  • If I run get_field() for the field in the filter inside the filter it causes an infinite loop.

    I’m using the filter to set the field[‘min’] and $field[‘max’] on a repeater field for a form, where I don’t want the users to be able to add a new row. (maybe there is a way to turn that off, but I have not seen other than setting the min/max to the length of rows in the repeater.

    Anyways, using get_post_meta() to get the length works fine, but if I were to replace that with get_field() it causes infinite loop and page dies.

    //FYI – sudo code to show the issue

    add_filter('acf/load_field/key=field_55b1208a40b0e', 'form_field_55b1208a40b0e');
    function form_field_55b1208a40b0e ( $field )
    {
      //* doesn't work
      $repeater_min_max = get_field( 'field_55b1208a40b0e', $submission_post->ID );
    
      //* works
      $repeater_min_max = get_post_meta( $submission_post->ID, 'display_all_sites', true );
     
      return $field;
    }

    Any insights, or just the way it is?

  • I’ve found that, as you say, it’s just the way it is. I discovered this when creating plugins that use ACF. Especially when getting a field that was attached to the same location as the field I was trying to dynamically generate. Caused quite a bit of forehead to screen action at the time.

    There is probably something that can be done about it. You could probably remove all of the filters and actions that are causing the infinite loop before you call get_field() and then add them again after you call get_field(). But this seems like a lot more work than using get_post_meta().

    You may also be able to do other things to stop it. Maybe create a check at the beginning of your function to make sure that you’re not doing your own action twice. I haven’t tried this. For me it was again just simpler to use get_post_meta

    Now when I’m creating a filter or action for ACF I just don’t use ACF functions because there is always the chance of creating the infinite loop when you do.

    Actually, these same types of infinite loops can be created in WP as well. For example, if you create a filter on the WP hook 'get_post_metadata' and then in that filter you call the function get_post_meta(), unless you do a few back flips to prevent it you set up an infinite loop situation.

  • Hey John,

    You are right, it could be like infinite loops in save_post hook. Thanks!

    https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

    But using the get_post_meta() is easy enough, just wanted to make sure it was either a known issue or if it was a bug that could be addressed.

    Edit…
    Yes, that actually works. Remove and then reset the filter inside the filter when calling get_field().

    //sudo code

    add_filter('acf/load_field/key=field_55b1208a40b0e', 'form_load_field_55b1208a40b0e');
    function form_load_field_55b1208a40b0e ( $field )
    {
      remove_filter( 'acf/load_field/key=field_55b1208a40b0e', 'form_load_field_55b1208a40b0e' );
      $repeater_min_max = get_field( 'display_all_sites', $submission_post->ID );
      add_filter('acf/load_field/key=field_55b1208a40b0e', 'form_load_field_55b1208a40b0e');
    
      return $field;
    } 
  • It was kinda hard to get to this thread, but John’s answer worked for me. Thanks!

  • Thank you for saving my sanity! Whew.

    I used the remove and add filter method above and it avoids the infinite looping.

  • Follow-up: the remove_filter/add_filter method ended up preventing the filter from running at all (even after re-adding), so get_post_meta() was the solution in my case.

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

The topic ‘get_field in field filter cause infinite loop’ is closed to new replies.