ACF programmatically add repeater

admin

Administrator
Staff member
I'm using <a href="http://advancedcustomfields.com" rel="nofollow">Advanced Custom Fields</a> (ACF) and trying to add a repeater programmatically to an existing group (group_5621b0871e1b1), but it doesn't work. The same code works for a text field but not for the repeater.

<strong>In my plugin:</strong>

Code:
add_action( 'acf/init', 'acf_add_field_royalties' );
function acf_add_field_royalties() {
    if ( function_exists( 'acf_add_local_field_group' ) ) {
        acf_add_local_field( array (
            'key' =&gt; 'field_store_royalties',
            'label' =&gt; 'Royalties',
            'name' =&gt; 'store_royalties1',
            'type' =&gt; 'repeater',
            'parent'       =&gt; 'group_5621b0871e1b1',
            'instructions' =&gt; '',
            'required' =&gt; 0,
            'conditional_logic' =&gt; 0,
            'wrapper' =&gt; array (
                'width' =&gt; '',
                'class' =&gt; '',
                'id' =&gt; '',
            ),
            'collapsed' =&gt; '',
            'min' =&gt; '',
            'max' =&gt; '',
            'layout' =&gt; 'table',
            'button_label' =&gt; 'Add new royalty period',
            'sub_fields' =&gt; array (
                array (
                    'key' =&gt; 'field_start_date',
                    'label' =&gt; 'Start Date',
                    'name' =&gt; 'start_date1',
                    'type' =&gt; 'date_picker',
                    'instructions' =&gt; '',
                    'required' =&gt; 1,
                    'display_format' =&gt; 'F j, Y',
                    'return_format' =&gt; 'd/m/Y',
                    'first_day' =&gt; 1,
                ),
                array (
                    'key' =&gt; 'field_end_date',
                    'label' =&gt; 'End date',
                    'name' =&gt; 'end_date1',
                    'type' =&gt; 'date_picker',
                    'instructions' =&gt; '',
                    'display_format' =&gt; 'F j, Y',
                    'return_format' =&gt; 'd/m/Y',
                    'first_day' =&gt; 1,
                ),
                array (
                    'key' =&gt; 'field_royalty_rate',
                    'label' =&gt; 'Royalty Rate',
                    'name' =&gt; 'royalty_rate1',
                    'type' =&gt; 'number',
                    'instructions' =&gt; '',
                    'required' =&gt; 1,
                    'wrapper' =&gt; array (
                        'width' =&gt; '',
                        'class' =&gt; '',
                        'id' =&gt; '',
                    ),
                    'default_value' =&gt; 0,
                    'placeholder' =&gt; '',
                    'prepend' =&gt; '',
                    'append' =&gt; '%',
                    'min' =&gt; 0,
                    'max' =&gt; 100,
                    'step' =&gt; 1,
                    'readonly' =&gt; 0,
                    'disabled' =&gt; 0,
                )
            )
        ));
    }
}

It shows this error in the group_5621b0871e1b1 group:

Code:
Warning: Invalid argument supplied for foreach() in /usr/share/nginx/html/wordpress4/wp-content/plugins/advanced-custom-fields-pro/pro/fields/repeater.php on line 255
Warning: Invalid argument supplied for foreach() in /usr/share/nginx/html/wordpress4/wp-content/plugins/advanced-custom-fields-pro/pro/fields/repeater.php on line 320

Am I doing something wrong?
Is it possible to add a repeater programmatically.