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
    	//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) ) {
    	//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){
    			case 'billboard':
    				$row_i = array( 'acf_fc_layout'=>'billboard', 'field_55ce0ed27c68a'=>$row->headline);
    			case 'callout':
    				$row_i = array( 'acf_fc_layout'=>'callout', 'field_564e22d0da4ef'=>$row->headline);
    			case 'content':
    				$row_i = array( 'acf_fc_layout'=>'content', 'field_55ce0e6c7c688'=>$row->headline);
    			case 'headline':
    				$row_i = array( 'acf_fc_layout'=>'headline', 'field_55df18296e4b3'=>$row->headline);
    			case 'recent':
    				$row_i = array( 'acf_fc_layout'=>'recent', 'field_55e5d3f2b7ec7'=>$row->headline);
    			case 'subheadline':
    				$row_i = array( 'acf_fc_layout'=>'subheadline', 'field_55df18be00e56'=>$row->headline);
    				//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.