Home Forums Add-ons Flexible Content Field Flexible content array_splice help


Flexible content array_splice help

  • Hi everyone.
    I want to insert a row layout from a Flexible Content field in one page (templates page) into the Flexible Content that is assigned to another page (Frontpage). The Flexible Content field group is the same. I am using the following code:

    $template = get_field('flexible_sections',3012);
    $frontpage = get_field('flexible_sections',796);
    $inserted = $template[1];
    array_splice($frontpage,0,0,$inserted );
    $field_key = "field_64ca5341d1168";
    update_field( $field_key, $frontpage, 796 );
    echo "<pre>";
    echo "<pre>";

    Although the array insertion occurs it seems its format is not correct and the template section is not recognized in the Frontpage. This is the array print:

        [0] => layout_section1
        [1] => Library: Section 2
        [2] => 
        [3] => 
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => Array
                [acf_fc_layout] => layout_section1
                [heading] => Frontpage: Section 1
                [image] => 
                [layout_name] => 
                [position] => 
                [subheading] => 
                [shortcode] => 
                [columns] => 

    Can someone point me the right direction?

  • The issue with getting a field in acf is that it returns an array using field_name => value pairs and that you must use field_key value pairs when you call update field to move copy it into a page, and this includes all sub fields.

    In order to copy an layout you must loop over both the place you want to copy it from and the place you want to copy it to. I don’t know the exact details of what you are doing so I can only give the basics. This example will copy all of the flex rows from one post to another post.

    // initialize rows to be updated
    $rows = array();
    // loop over the flex field on the post where you want to copy fields from
    if (have_rows('flex_field', $post->id)) {
      while (have_rows('flex_field', $post_id)) {
        // the_row returns a value
        // this value is an array of field_key => value pairs
        // including any nested fields
        $rows[] = the_row();
    // then loop over the fields you want to copy to
    // the same as above using a different post ID
    // update the flex field on the post you want to update
    update_field($field_key, $rows, $copy_to_post_id);
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.