Support

Account

Home Forums Add-ons Repeater Field Populate Choices in Repeater

Solving

Populate Choices in Repeater

  • Hi,

    I’ve got a field (select) within a repeater that I’m displaying on an options page. I’m trying to dynamically populate the choices – using this..

    function page_for_post_type($field)
    {
    	if(is_admin())
    	{
    		$field['choices']	= GetPostTypes();
    	}
    
    	return $field;
    }
    add_filter('acf/load_field/key=field_55db1d354ef79', __NAMESPACE__.'\\page_for_post_type');

    something like this works fine when I’m populating fields outside of a repeater. But in a repeater, this function/filter never seems to be called.

    I think this is because ACF if getting a cached version of the field, as far as I can see, in “api-field.php” @845 (ish) it loads $cache_key = "get_field/{$type}={$selector}"; from the cache, and returns before the acf/load_field filters get called.

    Am I doing something wrong here? Is there another way of doing this?

    Cheers

  • I tried your code but I get a fatal error, GetPostTypes() is not a function. What does this function do.

    I tried the following and it seems to be working as expected, the field key is for a select field in a repeater that I set up in some test fields.

    
    function page_for_post_type($field) {
      if(is_admin()) {
        $field['choices'] = array('choice 1','choice 2','choice 3');
      }
      return $field;
    }
    add_filter('acf/load_field/key=field_55edb28fac7a3', 'page_for_post_type');
    
  • Hi, the function pulls back the current post types available. With your example, if you were to add another value to the array now, and visit the options page (not field the editor view) you should find that the new values don’t show up in the select… Or that’s at least what’s happening for me. This function only seems to run and get populated when you try and edit the repeater.

    Cheers

  • Actually, I have this set up on an options page and every time I changes the array and reload the options page the new values in the array appear for me. The ACF cache that you’re refering to uses the WP caching mechanism and that only saves values during the same request, it’s not persistent.

    Do you have some other type of cache installed on the site?

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

The topic ‘Populate Choices in Repeater’ is closed to new replies.