2

I want to add a row to my posts flexible content field. I create my ACF fields using the code from the export tool, this is a part of it:

acf_add_local_field_group(array(
        'key' => 'group_5c4c39a08349a',
        'title' => 'Posts page',
        'fields' => array(
            array(
                'key' => 'field_5c4c39aab1ed4',
                'label' => 'Blokken positie',
                'name' => 'block_position',
                'type' => 'radio',
                'instructions' => 'Geef aan waar de pagina blokken geplaatst moeten worden',
                'required' => 1,
                'conditional_logic' => 0,
                'wrapper' => array(
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'choices' => array(
                    0 => 'Boven de berichten',
                    1 => 'Onder de berichten',
                ),
                'allow_null' => 0,
                'other_choice' => 0,
                'default_value' => 1,
                'layout' => 'vertical',
                'return_format' => 'value',
                'save_other_choice' => 0,
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_type',
                    'operator' => '==',
                    'value' => 'posts_page',
                ),
            ),
        ),
        'menu_order' => 6,
        'position' => 'normal',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen' => '',
        'active' => true,
        'description' => '',
    ));

    acf_add_local_field_group(array(
        'key' => 'group_5c4a061a6be68',
        'title' => 'Page/ Post blocks',
        'fields' => array(
            array(
                'key' => 'field_5c4a0645c3baa',
                'label' => 'Blocks',
                'name' => 'blocks',
                'type' => 'flexible_content',
                'instructions' => '',
                'required' => 0,
                'conditional_logic' => 0,
                'wrapper' => array(
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'layouts' => array(
                    'layout_5c4a084c7953b' => array(
                        'key' => 'layout_5c4a084c7953b',
                        'name' => 'news_slider',
                        'label' => 'Nieuws slider',
                        'display' => 'block',
                        'sub_fields' => array(
                            array(
                                'key' => 'field_5c4a085f7953e',
                                'label' => 'Type',
                                'name' => 'type',
                                'type' => 'select',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'choices' => array(
                                    0 => 'Laatste berichten',
                                    1 => 'Berichten uit een categorie',
                                    2 => 'Zelf de berichten selecteren',
                                ),
                                'default_value' => array(
                                ),
                                'allow_null' => 0,
                                'multiple' => 0,
                                'ui' => 0,
                                'return_format' => 'value',
                                'ajax' => 0,
                                'placeholder' => '',
                            ),
                            array(
                                'key' => 'field_5c4ac2d1986b9',
                                'label' => 'Aantal nieuws artikelen',
                                'name' => 'news_items',
                                'type' => 'number',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => array(
                                    array(
                                        array(
                                            'field' => 'field_5c4a085f7953e',
                                            'operator' => '==',
                                            'value' => '0',
                                        ),
                                    ),
                                    array(
                                        array(
                                            'field' => 'field_5c4a085f7953e',
                                            'operator' => '==',
                                            'value' => '1',
                                        ),
                                    ),
                                ),
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => 9,
                                'placeholder' => '',
                                'prepend' => '',
                                'append' => '',
                                'min' => 3,
                                'max' => 18,
                                'step' => 3,
                            ),
                            array(
                                'key' => 'field_5c4a093b2ba54',
                                'label' => 'Toon nieuws uit categorie',
                                'name' => 'news_category',
                                'type' => 'taxonomy',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => array(
                                    array(
                                        array(
                                            'field' => 'field_5c4a085f7953e',
                                            'operator' => '==',
                                            'value' => '1',
                                        ),
                                    ),
                                ),
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'taxonomy' => 'category',
                                'field_type' => 'select',
                                'allow_null' => 0,
                                'add_term' => 1,
                                'save_terms' => 0,
                                'load_terms' => 0,
                                'return_format' => 'object',
                                'multiple' => 0,
                            ),
                            array(
                                'key' => 'field_5c4a09e7ee82f',
                                'label' => 'Selecteer nieuws berichten',
                                'name' => 'news_item_collection',
                                'type' => 'repeater',
                                'instructions' => 'De slider laat 3 items per slide zien. Zorg dat het aantal items deelbaar is door 3.',
                                'required' => 0,
                                'conditional_logic' => array(
                                    array(
                                        array(
                                            'field' => 'field_5c4a085f7953e',
                                            'operator' => '==',
                                            'value' => '2',
                                        ),
                                    ),
                                ),
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'collapsed' => '',
                                'min' => 0,
                                'max' => 0,
                                'layout' => 'table',
                                'button_label' => '',
                                'sub_fields' => array(
                                    array(
                                        'key' => 'field_5c4a0a08ee830',
                                        'label' => 'Nieuws item',
                                        'name' => 'news_item',
                                        'type' => 'post_object',
                                        'instructions' => '',
                                        'required' => 0,
                                        'conditional_logic' => 0,
                                        'wrapper' => array(
                                            'width' => '',
                                            'class' => '',
                                            'id' => '',
                                        ),
                                        'post_type' => array(
                                            0 => 'post',
                                        ),
                                        'taxonomy' => '',
                                        'allow_null' => 0,
                                        'multiple' => 0,
                                        'return_format' => 'object',
                                        'ui' => 1,
                                    ),
                                ),
                            ),
                        ),
                        'min' => '',
                        'max' => '',
                    ),
                    'layout_5c4a065576f71' => array(
                        'key' => '5c4a065576f71',
                        'name' => 'text',
                        'label' => 'Tekst (1 blok per rij)',
                        'display' => 'block',
                        'sub_fields' => array(
                            array(
                                'key' => 'field_5c4a0666c3bab',
                                'label' => 'Tekst',
                                'name' => 'text_content',
                                'type' => 'wysiwyg',
                                'instructions' => 'Het wordt afgeraden om in deze editor afbeeldingen in te voegen. Gebruik bij voorkeur het block "Afbeelding" hiervoor.',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'tabs' => 'visual',
                                'toolbar' => 'full',
                                'media_upload' => 1,
                                'delay' => 0,
                            ),
                        ),
                        'min' => '',
                        'max' => '',
                    ),

When I create or edit a post I can see and use these fields. Now I have a script to import posts and I want to add the field Tekst (1 blok per rij) as a new row programmatically. I've tried using add_sub_row and update_field but none worked properly.

When I use add_sub_row like this I always get a return value of false:

add_sub_row([ 'field_5c4a0645c3baa', 1, 'field_5c4a0666c3bab'], [
    'field_5c4a0666c3bab' => 'test'
], $post)

When I use update_field I do get a response which makes it look like the field has been added, but I can't see it when I go to wp-admin and open the post:

update_field('field_5c4a0645c3baa', [
    'acf_fc_layout' => 'text',
    'text_content' => 'test'
], $post)

How should I use these methods? Because I've tried to use the manual of ACF for it but I don't really understand how to put it to use on flexible content.

  • did you try `add_row()` instead of sub row? or maybe `update_row()` ? – Stender Mar 28 '19 at 07:56
  • I really can't find any counterpart to get_row_layout, so this might be a question for the support page itself. But I have looked through the forum, and people have been asking this question for years.. So you might not get an answer there either. – Stender Mar 28 '19 at 08:14
  • related questions : https://support.advancedcustomfields.com/forums/topic/programmatically-adding-layouts-to-a-flexible-content-field/ - https://support.advancedcustomfields.com/forums/topic/programatically-adding-content-into-flex-content-fields/ – Stender Mar 28 '19 at 08:14
  • - and overall support for flexible content : https://support.advancedcustomfields.com/forums/forum/add-ons/flexible-content-field/ – Stender Mar 28 '19 at 08:15

1 Answers1

3

I've contacted the ACF support. They told me this:

Thanks for reaching out to us.

Unfortunately, I'm afraid it's not possible to add using the add_row function with the flexible content field.

However, you could implement a custom solution using the update_field function to add sub_rows to the flexible content field.

The developer already has it in his todo and probably this option will be available in future updates.

Apologies for the inconvenience.

Later they replied with this:

Thanks for the follow up.

Ideally, you need to load the field, modify the field values and update the field using update_field.

Kindly have a look at the following article from the support forums.

https://support.advancedcustomfields.com/forums/topic/issues-with-add_rows-and-flexible-content/

Hope this helps.

The link helped me to create a new row in the flexible content.

Using this code:

$row_i = add_row(
    'field_55ce0e107c687', 
    [
        'acf_fc_layout' => 'headline', 
        'field_55df18296e4b3' => 'headline text'
    ],
    $ID
);

So I had to adjust it to my field names:

add_row(
    'field_5c4a0645c3baa', [
    'acf_fc_layout'       => 'text',
    'field_5c4a0666c3bab' => $block[ 'value' ],
], $post);

Now it works fine.

  • also if you want to add repeater row inside flexible content then, `$data[] = array( 'field_5e7349062f311' => 'title 2', 'field_5e73490f2f312' => 'std pax 2', ); /* repeater */ add_row('field_5e872ef537705', array( 'acf_fc_layout'=>'enter_rates','field_5e73527e242eb'=>'2023','field_5e7348c32f310'=>$data), 8374);` – Chirag Rathore May 27 '20 at 10:28