Support

Account

Home Forums ACF PRO Using acf/load_value with Options Pages

Helping

Using acf/load_value with Options Pages

  • Has anyone used acf/load_value to pre-populate a bunch of values to a field (in this case, a repeater) on an Options page? I can’t get this to work. No errors thrown, but no magic happening either.

    This happens to be a Network install.

    I’ve checked that the key names are correct, and now I’m stumped! My best guess here is that because we need to pass the post_id and this is not a post/page, it’s not going to work … ?

    Current (failing) code below.

    add_filter('acf/load_value/key=field_59e3c01dff5d3', 'ek_option_defaults', 10, 3);
      function ek_option_defaults($value, $post_id, $field) {
          if ($value === false) {
              $value = array(
                  array(
    		            'field_59e3c02cff5d4' => 'FCCLA',
    		            'field_59e3c031ff5d5' => 'Family, Career and Community Leaders of America',
              );
          }
          return $value;
      }
    
  • Hi

    the filter ‘acf/load_value’ is used when you are calling ‘get_field’ in the frontend i believed. The proper filter you are looking for is ‘acf/prepare_field’.
    https://www.advancedcustomfields.com/resources/acf-prepare_field/

    So, your code would look something like:

     
    
    add_filter('acf/prepare_field/key=field_5a17b5c21950f', 'ek_option_defaults');
    function ek_option_defaults($field) {
        if ($field['value'] === false) {
            $field['value'] = [
                [
                    'field_59e3c02cff5d4' => 'a',
                    'field_59e3c031ff5d5' => 'a'
                ],
                [
                    'field_59e3c02cff5d4' => 'b',
                    'field_59e3c031ff5d5' => 'b'
                ],
            ];
        }
    
        return $field;
    }
    

    Cheers. 😁

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

The topic ‘Using acf/load_value with Options Pages’ is closed to new replies.