Support

Account

Home Forums ACF PRO Adding repeater items inside of Flexible Content Field using PHP Script

Solved

Adding repeater items inside of Flexible Content Field using PHP Script

  • I am scraping some albums from a third-party site and then inserting them into WordPress. I have numerous fields using Advanced Custom Fields Pro and the Flexible Content Layout functionality. My issue is I cannot understand how I can insert multiple items in a repeater which is in a Flexible Content Layout.

    My structure is as follows:

    
    page_sections (Flexible Content)
        
        Label: Tracklist
        Name: tracklist
        Display: Row
    
            title (Text)
            tracks (Repeater)
                track_name (Text)
                track_length (Text)
    

    I came across the documentation http://www.advancedcustomfields.com/resources/update_field/ for the update_field() method which has some examples for updating/inserting data. One of those examples is for inserting a Flexible content row.

    While I understand how it would work for simple sub-fields like text, how would I go about adding in multiple items which are in the repeater that sits within my Flexible content row?

  • You’ll need to combine the one for the flexible content row with the one for the repeater row just above it.

    
    $field_key = "flexible_field";
    $value = get_field($field_key);
    $value[] = array(
        "sub_field_1" => "Foo1",
        "sub_field_2" => "Bar1",
        "repeater_field" => array(
            array(
                'repeater_sub_1' => 'value',
                'repeater_sub_2' => 'value'
            )
        ),
        "acf_fc_layout" => "layout_1_name"),
    ;
    update_field( $field_key, $value, $post_id );
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Adding repeater items inside of Flexible Content Field using PHP Script’ is closed to new replies.