Support

Account

Home Forums Backend Issues (wp-admin) Pre-populate mulitiple Repeater rows

Solved

Pre-populate mulitiple Repeater rows

  • Hi there,

    I’m using a repeater field with two sub fields: Caption (select) and Team Member (Taxonomy).

    I’d like to pre-populate something like 8 rows with the Caption (select), so the editor doesnt have to add it manually each time.

    I searched the forums and found this, but it doesnt work:

    // Add pre-populated Repeater Fields
    function my_acf_load_value($value, $post_id, $field) {
      if ($value !== NULL) {
        // if the value is exactly NULL it means
        // the field has never been updated
        // we don't want to change fields that have already been editied
        return $value;
      }
      // set the new field value
      $value = array(
        // add a nested array for each row
        array(
          // add an array item for each sub field
          'caption' => 'Production',
          'team_member' => 'Test'
        ),
        array(
          // add an array item for each sub field
          'caption' => 'Advertiser',
          'team_member' => 'Test2'
        )
      );
      return $value;
    }
    add_filter('acf/load_value/name=team', 'my_acf_load_value', 20, 3);

    Any suggestions?

  • Two things you can try,

    1) Lower the priority of your filter to < 10 add_filter('acf/load_value/name=team', 'my_acf_load_value', 1, 3);
    2) Try using the field key instead of the field name add_filter('acf/load_value/jkey=field_XXXXXXX', 'my_acf_load_value', 20, 3);

  • Hi John,

    Unfortunately both options did nothing to the fields.

    Adding some echo text in between the function makes it seem like it’s not running at all? Any other way to test this?

    Thanks!

  • If it’s not running at all then that’s another issue. Where is your code located? Place this at the top of the function to see if anything is output to know if the filter is running

    
    var_dump($value);
    
  • Hi John,

    I’ve tried this on a fresh install and it doesn’t work either.

    • When I comment out if if ($value !== NULL) it at least gets to the $value array. If I don’t, it always seems to return NULL.
    • The $value array runs, but only creates a repeater for each array (2 in the example) but the fields themselves are empty.
    • Var dump returns array(2) { [0]=> array(2) { ["title"]=> string(10) "Production" ["member"]=> string(7) "Testone" } [1]=> array(2) { ["title"]=> string(6) "Artist" ["member"]=> string(7) "Testtwo" } }

    Thank you for your efforts so far!

    • Test the value of an empty repeater, it may be NULL or it may be false. Check this by placing var_dump($value); at the top of your filter.
    • You must use field keys to populate the repeater and not field names. This is why the rows are created but have no values.
  • Thanks John,

    The empty $value returns bool(false).

    I currently use the following which seems to work:

    // Add pre-populated Repeater Fields
    function my_acf_load_value($value, $post_id, $field) {
      
      if ($value !== FALSE) {
        return $value;
      }
      $value = array(
        array(
          'field_5e8317693aa96' => 'Production',
        ),
        array(
          'field_5e8317693aa96' => 'Director',
        ),
        array(
          'field_5e8317693aa96' => 'Author',
        ),
        array(
          'field_5e8317693aa96' => 'Artist',
        ),
        array(
          'field_5e8317693aa96' => 'Etc'
        )
      );
      return $value;
    
    }
    add_filter('acf/load_value/name=team', 'my_acf_load_value', 1, 3);

    Thanks a bunch!

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

The topic ‘Pre-populate mulitiple Repeater rows’ is closed to new replies.