16. Group Fields | Field Forge - Custom Fields, Built for Speed
Download Log in

16. Group Fields

Developer Guide

Group fields bundle related sub-fields into a single named unit. Unlike repeaters (which have multiple rows), a group always has exactly one set of sub-field values. Groups are ideal for structured data blocks like addresses, hero settings, or SEO meta.

Accessing Group as Array

php
// Method 1: use have_rows (recommended)
if ( have_rows( 'address', 42 ) ) :
    while ( have_rows( 'address', 42 ) ) : the_row();
        $street = get_sub_field( 'street' );
        $city   = get_sub_field( 'city' );
        $state  = get_sub_field( 'state' );
        $zip    = get_sub_field( 'zip' );
    endwhile;
endif;

echo '<address>';
echo esc_html( $street ) . '<br>';
echo esc_html( $city ) . ', ' . esc_html( $state ) . ' ' . esc_html( $zip );
echo '</address>';

// Method 2: Direct get_field returns the whole group as an array
$address = get_field( 'address', 42 );
if ( $address ) {
    echo esc_html( $address['street'] ) . '<br>';
    echo esc_html( $address['city'] ) . ', ' . esc_html( $address['state'] );
}

Group for Layout/Style Settings

php
<?php
if ( have_rows( 'hero_settings' ) ) :
    while ( have_rows( 'hero_settings' ) ) : the_row();
        $bg_color   = get_sub_field( 'background_color' );
        $text_color = get_sub_field( 'text_color' );
        $overlay    = get_sub_field( 'overlay_opacity' );
        $bg_image   = get_sub_field( 'background_image' );
    endwhile;
endif;
?>

<section class="hero"
    style="background-color: <?php echo esc_attr( $bg_color ); ?>;
           color: <?php echo esc_attr( $text_color ); ?>;
           <?php if ( $bg_image ) : ?>
               background-image: url(<?php echo esc_url( $bg_image['url'] ); ?>);
           <?php endif; ?>">
    <div class="hero-content">
        <h1><?php the_field( 'hero_heading' ); ?></h1>
    </div>
</section>

Programmatic Group Access

php
// Update group sub-fields
update_field( 'address', [
    'street' => '123 Main St',
    'city'   => 'Portland',
    'state'  => 'OR',
    'zip'    => '97201',
], 42 );

// Read and modify a group
$address = get_field( 'address', 42 );
$address['zip'] = '97202';
update_field( 'address', $address, 42 );

Forge AI Assistant Online

Hi! I'm the Field Forge AI assistant. Ask me anything about the plugin — setup, features, troubleshooting, or development.

Just now
Powered by Forge AI · Browse docs