4

Wondering how to use add_rows (or something similar) to add an entry to a flexible content field programmatically. On ACF's website they mention add_rows can be used to add a row to a flexible content field https://www.advancedcustomfields.com/resources/ but they give no examples how to do it with a flexible content field; only with a repeater field. Thanks!

hot_barbara
  • 522
  • 1
  • 8
  • 19

1 Answers1

3

use add_row( $selector, $value, $post_id ) This function will add a new row of data to an existing repeater field field value.

$selector: (required) The parent field name or key

$value: (required) The new value to append

$post_id: (optional) The post ID of which the value is saved to. Defaults to current post Return

Below is the example that how you can add image filed with multiple values

<?php 

$row = array(
    'image' => 123,
    'alt'   => 'Another great sunset',
    'link'  => 'http://website.com'
);

$i = add_row('images', $row);

?>

To Add repeater field in flexible content you should use below code :

<?php 
$field_key = "flexible_content_field_key";
$value = array(
    array( "sub_field_1" => "Foo1", "sub_field_2" => "Bar1", "acf_fc_layout" => "layout_1_name" ),
    array( "sub_field_x" => "Foo2", "sub_field_y" => "Bar2", "acf_fc_layout" => "layout_2_name" )
);
update_field( $field_key, $value, $post_id );

?>

Here acf_fc_layout is used to add sub fields for image here image is flexible content field key where in your case there will be your flexible content key.

raju_odi
  • 1,433
  • 13
  • 29
  • Yes I've seen this example but this is an example for a repeater field, not a flexible content field...like I mentioned in the OP. Thanks anyway though. – hot_barbara Sep 27 '18 at 07:05
  • 1
    I have updated the answer for add repeater fields in flexible content fields – raju_odi Sep 27 '18 at 07:13
  • That's what's up! You're my hero! – hot_barbara Sep 27 '18 at 07:25
  • fwiw I reached out to ACF support and they told me that add_rows is actually NOT supported for flexible content fields despite what their site says at this current time. Support also mentioned this is on the developer's todo list. – hot_barbara Sep 27 '18 at 20:26