Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use add_row to programmatically add an entry to a flexible content field in advanced custom fields for wordpress

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!

like image 364
hot_barbara Avatar asked Oct 25 '25 00:10

hot_barbara


1 Answers

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.

like image 126
raju_odi Avatar answered Oct 26 '25 15:10

raju_odi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!