Support

Account

Home Forums ACF PRO Error getting option even though get_field function exists

Solved

Error getting option even though get_field function exists

  • ACF Pro 5.8.3

    I’m using the determine_current_user filter to set the current user. It works fine until I try to retrieve an option with get_field, then I get an out of memory error from WordPress.

    Allowed memory size of 134217728 bytes exhausted (tried to allocate 8192 bytes) in
    <b>/sites/wordpress.test/wp-includes/class-wp-query.php</b> on line <b>596</b>
    
    <b>Fatal error</b>: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65536 bytes) in
    <b>/sites/wordpress.test/wp-includes/functions.php</b> on line <b>3225</b>

    So I wrapped the code in a check for the get_field function, and although the function exists the error keeps happening.

    If the get_field function exists why can’t the option be retrieved?

    add_filter('determine_current_user', 'set_user');
    
    function set_user($user_id)
    {
      if (function_exists('get_field')) {
        $user = get_field('user_id', 'option');
      } else {
        $user = 0;
      }
    
      return $user;
    }

    So I’ve been retrieving it directly with get_option, which works fine:

    $user = get_option('options_user_id');

    FYI, it also fails with same error when retrieving the value from a post:

    $user = get_field('user_id', 79);

  • You are probably creating an infinite loop with your filter. More than likely your call to get_field() is inadvertently causing your filter to be called again, but this is just a guess assuming that everything runs fine when you remove your filter.

    What you need to do is remove your filter at the top of the filter and than add it back in before returning.

    
    add_filter('determine_current_user', 'set_user');
    
    function set_user($user_id)
    {
      remove_filter('determine_current_user', 'set_user');
      if (function_exists('get_field')) {
        $user = get_field('user_id', 'option');
      } else {
        $user = 0;
      }
      add_filter('determine_current_user', 'set_user');
    
      return $user;
    }
    
  • Thanks. That was it.

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

You must be logged in to reply to this topic.