WPLake > Learning Hub > ACF Group Field

ACF Group Field

Enhance field organization in ACF: Discover the Group field for contextual grouping, differentiate from Field Group, and optimize database structuring.

Key Points at a Glance

  1. Organizing fields: The ACF Group field assists in organizing fields into context-related groups, making navigation easier for editors and providing better structure.
  2. Distinction between ACF Field Group and ACF Group Field: Although similar in name, the ACF Field Group and ACF Group Field serve different purposes, with the former acting as a global field container and the latter as a local one.
  3. Limitation on repetition: Unlike the Repeater field, the Group field does not offer the option to repeat the same set of fields multiple times, limiting its functionality in scenarios requiring repeated sets of fields.
  4. Database structuring: The Group field structures fields not only for editors but also in the database by adding a group name prefix to all fields, ensuring scoping and avoiding potential name conflicts.
  5. Querying and displaying field values: Field values from the Group field can be retrieved using the get_field function with the group name, ensuring scoped retrieval and avoiding conflicts.
  6. Alternative field options: For scenarios requiring more flexibility in field repetition, the Flexible field may be a better alternative to the Group field, allowing for multiple sets of fields within one field container.
  7. Advanced Views plugin integration: Utilizing smart templates from the Advanced Views plugin can streamline the querying and displaying of ACF fields, including complex ones like Group fields, while ensuring code scoping and reusability.

Table of Contents

Intro

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

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

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

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

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.

Frequently Asked Questions

  1. How does the ACF Group field differ from the Repeater field?

    Unlike the Repeater field, the ACF Group field does not allow for the repetition of the same set of fields multiple times, limiting its functionality in scenarios requiring repeated sets of fields.

  2. How are fields structured in the database by the ACF Group field?

    The ACF Group field structures fields in the database by adding a group name prefix to all fields, ensuring scoping and avoiding potential name conflicts.

  3. How can I query and display field values from the ACF Group field?

    Field values from the ACF Group field can be retrieved using the get_field function with the group name, ensuring scoped retrieval and avoiding conflicts.

Content links (16)

Related articles

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