Support

Account

Home Forums Add-ons Flexible Content Field How to use an Image Field within Flexible Content to set the post's featured img Reply To: How to use an Image Field within Flexible Content to set the post's featured img

  • Hi @byoo

    Please keep in mind that the value of flexible content has a different structure. So it should be something like this:

    function my_acf_save_post( $post_id ) {
        
        // get the flexible content raw values
        $flexible_content = get_field('flexible_content_field_name', $post_id, false);
        
        // check if the post has flexible content or not
        if( $flexible_content ){
            
            // loop through the values
            foreach($flexible_content as $row){
                
                // check if the image is set in certain layout
                // please change 'layout_name' with your layout name and
                // 'field_1234567890abc' with the image field key
                if( $row['acf_fc_layout'] == 'layout_name' && isset($row['field_1234567890abc']) ){
                    
                    // update the featured image
                    update_post_meta($post_id, '_thumbnail_id', $row['field_1234567890abc']);
                }
            }
        }
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    Where “flexible_content_field_name” is the flexible content field name, “layout_name” is the layout name where the image field is located, and “field_1234567890abc” is the image field key.

    I hope this helps πŸ™‚