ACF programmatically add repeater

I’m using Advanced Custom Fields (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.

In my plugin:

Read More
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' => 'field_store_royalties',
            'label' => 'Royalties',
            'name' => 'store_royalties1',
            'type' => 'repeater',
            'parent'       => 'group_5621b0871e1b1',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array (
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'collapsed' => '',
            'min' => '',
            'max' => '',
            'layout' => 'table',
            'button_label' => 'Add new royalty period',
            'sub_fields' => array (
                array (
                    'key' => 'field_start_date',
                    'label' => 'Start Date',
                    'name' => 'start_date1',
                    'type' => 'date_picker',
                    'instructions' => '',
                    'required' => 1,
                    'display_format' => 'F j, Y',
                    'return_format' => 'd/m/Y',
                    'first_day' => 1,
                ),
                array (
                    'key' => 'field_end_date',
                    'label' => 'End date',
                    'name' => 'end_date1',
                    'type' => 'date_picker',
                    'instructions' => '',
                    'display_format' => 'F j, Y',
                    'return_format' => 'd/m/Y',
                    'first_day' => 1,
                ),
                array (
                    'key' => 'field_royalty_rate',
                    'label' => 'Royalty Rate',
                    'name' => 'royalty_rate1',
                    'type' => 'number',
                    'instructions' => '',
                    'required' => 1,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => 0,
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '%',
                    'min' => 0,
                    'max' => 100,
                    'step' => 1,
                    'readonly' => 0,
                    'disabled' => 0,
                )
            )
        ));
    }
}

It shows this error in the group_5621b0871e1b1 group:

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.

Related posts

1 comment

  1. Since the repeater field was added using acf_add_local_field, each subfield needs to be added with acf_add_local_field as well.

    • Delete all of the subfields, including the 'subfields' => array
      line, from the repeater field.
    • Add each subfield in a new acf_add_local_field
    • When adding subfields, Make sure to add 'parent' => 'repeater_field_key' to the subfields.

    Your code would now look like:

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

Comments are closed.