WPLake > Blog > ACF Group Field: 3 Things You Should Know

ACF Group Field: 3 Things You Should Know

Time to read: 4 mins

-

ACF Plugins Tips and Tricks

-

Updated 19.03.24

Learn how to use the Group field to better organize and structure your fields.

Table of Contents

Intro

#link copied

Advanced Custom Fields (ACF) is one of the most famous meta-field plugins. The Group field type is a Layout field type, which allows for better organization and structuring of fields, but it doesn't have its own field input.

Note: Both the ACF Field Group and ACF Group Field sound similar but mean different things. Both act as field containers, while the ACF Field Group acts as a global field container, and ACF Group Field as a local one.

Therefore, the ACF Field Group can contain any fields, including ACF Group fields, and can be attached to admin screens, while the ACF Group Field is a local field container that groups fields only inside the global group.

ACF Group field
The ACF Group field is a container for other fields within a local scope

The layout setting allows you to customize the appearance of fields within the group. There are three options available: Block, Row, and Table.

Block layout of the ACF Group Field
The "Block" option in the layout settings stacks all inner items, including the labels, into a column
Row layout of the ACF Group field
The "Row" option in the layout settings arranges every field into a horizontal row
Table layout of the ACF Group field
The "Table" option in the layout settings enables a table appearance, with field labels serving as column names

Now, let's go through three main points to understand the field behavior.

1. Assists in organizing fields

#link copied

The primary goal of the ACF Group field is to assist in organizing fields and providing better structure for editors. When we have a lot of fields on the same page, it may be difficult to navigate through them. The Group field allows us to organize them into context-related groups, making navigation easier for editors.

Let's consider a practical example: suppose we have a Company CPT, with primary company information fields such as year of creation, number of workers, etc., along with contact details like address, opening hours, etc., and testimonials and reviews.

This already sounds like an extensive set of fields. If we place them independently in random order, it's going to be complex for editors to find the target fields and amend the information.

By using the Group field, we can combine related fields into groups; in our case, company information (year of creation, number of workers, etc.), contact details (address, opening hours), and testimonials (testimonials and reviews).

In this way, editors can focus on the fields inside the target group without jumping around in search of the field or keeping all section-related fields in mind.

2. Can't be repeated

#link copied

It's important to note that the Group field doesn't offer the 'repeater' option. If you need a repeatable group of fields, you should consider using the Repeater field, which is similar to the Group field but allows you to repeat the same set of fields any number of times.

If you're interested in providing more flexibility to editors, you might want to consider using the Flexible Content field. This field acts like a repeater with several sets of groups, allowing editors to use some of them, repeat them, and combine them in their own way.

Tip: Inside the group, you can reuse any existing set of fields by using the Clone field.

3. Wraps field values

#link copied

The group field structures fields not only for editors but also in the database. It adds the group name as a prefix to all fields, allowing you to easily identify which group the field belongs to and avoid potential name conflicts.

ACF Group sub fields in DB
All subfields inside the group are saved into the database with the group prefix

Consider this fact if you need to make a query by meta fields or display them. To retrieve the group field values, you should call the get_field function with the name of the group, and it will return an array with the group field values.

<?php

// todo your group field name here
$group = get_field( 'my_group' );

// todo use your subfield names here
$first_name = $group['first_name'] ?? '';
$last_name  = $group['last_name'] ?? '';

This approach ensures that the fields are scoped, avoiding any potential name conflicts. The scoping that the Group field offers is an important aspect of web development and is considered a best practice.

You should scope not only fields but also templates that display them, along with their styles.

Utilizing smart templates from the Advanced Views plugin, you can effortlessly query and display ACF fields with any field type, including complex ones like Group, while keeping your components reusable.

Summary

#link copied

The ACF Group field allows you to organize your fields, providing better structure for editors and avoiding field name conflicts.

Although the ACF Field Group and ACF Group Field sound similar, they have different functionalities. Both act as containers, with ACF Field Group serving as a global field container and ACF Group Field as a local one.

The Group field, like the Repeater field, contains a fixed set of subfields but they cannot be repeated. If you want to give editors more freedom and allow for operating several different sets of fields within one field, consider using the Flexible field.

Scoping provided by the Group field is an important aspect of web development and is considered a best practice. Remember to scope all your code, including scripts and styles. Alternatively, consider using smart templates from the Advanced Views plugin, which offers hassle-free scoping out-of-the-box.

About the Author
Maxim Akimov

Certified WordPress expert from Ukraine with over 8 years of experience. Advocate of the BEM methodology and the overall modular approach. Loves sporting activities and enjoys going to the gym and regularly plays table tennis.

0 Comments

    Leave a comment

    Reply to 

    Please be considerate when leaving a comment.

    Not shown publicly

    Got it