Home › Forums › Add-ons › Flexible Content Field › Issues with add_rows and flexible content › Reply To: Issues with add_rows and flexible content
Sweet, I’ve got it figured out! update_field
was the trick.
Before I was looping through a bunch of rows and attempting to add them each individually with add_row
, but now I’m looping through and creating an array of fields and then add them all at once with update_field
. This works because I’m only doing this on new pages and therefore the flexible content field is always empty. I imagine that if there was any content in the FC at the time this was run then the existing content would be overridden. Here’s the updated code that’s working for reference and posterity:
function bagpress_sitemapper_new_page( $ID, $post, $update ) {
// set content and modules as set in sitemap/json
if ( $post->post_type !== 'page' ||
$post->post_status === 'auto-draft' ||
get_post_meta('modules') ) {
//if not a page or an autodarft or has already been updated or has any content in the modules already, don't update it. get out!
//use modules custom field as the flag since it will exist automatically in there are any modules. and in that case we don't want to add any more
return;
}
//read module json data from the content
$content = json_decode( get_post_field('post_content', $ID) );
//if not array/json end this
if ( !is_array($content) ) {
return;
}
//flexible content field key
$fc_key = 'field_55ce0e107c687';
$fc_rows = [];
//read json stored in content for this post and use it to add rows to the flexible content
foreach($content as $row){
switch($row->layout){
case 'billboard':
$row_i = array( 'acf_fc_layout'=>'billboard', 'field_55ce0ed27c68a'=>$row->headline);
break;
case 'callout':
$row_i = array( 'acf_fc_layout'=>'callout', 'field_564e22d0da4ef'=>$row->headline);
break;
case 'content':
$row_i = array( 'acf_fc_layout'=>'content', 'field_55ce0e6c7c688'=>$row->headline);
break;
case 'headline':
$row_i = array( 'acf_fc_layout'=>'headline', 'field_55df18296e4b3'=>$row->headline);
break;
case 'recent':
$row_i = array( 'acf_fc_layout'=>'recent', 'field_55e5d3f2b7ec7'=>$row->headline);
break;
case 'subheadline':
$row_i = array( 'acf_fc_layout'=>'subheadline', 'field_55df18be00e56'=>$row->headline);
break;
default:
//unknown layout type - skip
// $row_i = update_post_meta($ID, 'module_error', json_decode($row) );
}
array_push($fc_rows, $row_i);
}
update_field($fc_key, $fc_rows, $ID);
//turn off save_post hook so we can update the post without triggering it
remove_action('save_post', 'bagpress_sitemapper_new_page', 10, 3);
//remove json data from content
wp_update_post( array(
'ID' => $ID,
'post_content' => ''
));
//turn save_post hook back on
add_action( 'save_post', 'bagpress_sitemapper_new_page', 10, 3);
}
add_action( 'save_post', 'bagpress_sitemapper_new_page', 10, 3 );
If one wanted to use update_field
to update a field that already contained existing content, I don’t think it’d be too hard to first read that field and put the contents into an array and then add the new rows/layouts to the array and call update_field
sending in the new array.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.