Support

Account

Home Forums Add-ons Flexible Content Field Save flexible content rows in post content Reply To: Save flexible content rows in post content

  • Hi John,
    thank you very much for the clear and huge explanation.

    At the moment the important task is to solve the question of putting the flexible content into the post_content.

    Actually I’m using the following solution that works very well for me. This solution is designed only for my pourposes but could be customized for other possible scenarios.

    function flexible_content_to_post_content( $post_id ) {
    
    	$post_type = 'books';
    
    	//Check if we are saving a books post type
    	if( get_post_type( $post_id ) != $post_type)
    		return;
    
    	//Check it's not an auto save routine
    	if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
    		return;
    
    	//The Post Content
    	$post_content = '';
    	
    	//Loop the flexible content rows
    	if( have_rows('flexible_content') ):
    		while ( have_rows('flexible_content') ) : the_row();
    		
    			//TEXT BLOCK
    			if( get_row_layout() == 'text_block' ):
    				
    				$post_content .= get_sub_field('wysiwyg');
    	
    			endif;
    				
    		endwhile;
    	endif;
    	
    	
        //If calling wp_update_post, unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'flexible_content_to_post_content');
    
    	// call wp_update_post update, which calls save_post again. E.g:
        wp_update_post(array('ID' => $post_id, 'post_content' => $post_content));
    
        // re-hook this function
        add_action('save_post', 'flexible_content_to_post_content');
    	
    }
    add_action('save_post', 'flexible_content_to_post_content');

    Thanks for sharing your plugin. At the moment the solution I’ve reported above is enough easy to use for me. I don’t need to check for all acf fields within the post. I need only to check text fields (textinput, textarea, wysiwyg) inside the flexible content.

    Of course I need to do some testing for large flexible fields, but considering my personal context, this should not be a problem.

    Thank you very much.