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

ACF User Field: 3 Things You Should Know

Time to read: 5 mins

-

ACF Plugins Tips and Tricks

-

Updated 21.03.24

Learn where the ACF User field stores values and how to enhance its functionality by activating the bidirectional feature to transform it into a superfield.

Table of Contents

Intro

#link copied

Advanced Custom Fields (ACF) is one of the best meta field plugins. The User field is categorized under the Relationship group fields, empowering editors to 'relate' one or more users to posts and custom post type (CPT) items. It can be viewed as an extension of the Select field specifically tailored for user objects.

The terms "user field type" and "user fields" may sound similar, but they refer to completely different concepts. The user field type refers to a type of field in ACF that allows users to choose and relate to any instance, including posts and CPT items.

On the other hand, "user fields" typically refer to ACF fields that are attached to the user screen. So, while the former refers to the type of field, the latter denotes the location of the field. For example, you can add the "user field" to the "user fields" screen for customization.

While WordPress posts and CPT items come with a built-in author field, the User field is designed to be more versatile. Both fields allow for the 'relation' of specific WordPress users to posts, but they serve different purposes.

The author field is automatically populated by WordPress with the user who authored the current post. Although it can be modified later, it can only contain one user. The choice is stored as a column in the posts table, ensuring efficient and rapid search capabilities.

On the other hand, the ACF User field is a custom meta field filled by editors. It can store one or more users, offering flexibility for indirect relationships, such as the Employees user field in the Company CPT.

The user field stores its value in the post meta table, which may impact performance when querying by this field. However, this limitation can be circumvented by utilizing the bidirectional feature.

User field settings

In addition to the traditional label and name settings, the user field settings include the "Filter by Role" option, which enables us to fine-tune the list of users available for selection based on their roles.

ACF User field look for editors
The field displays a select-like appearance for editors, including user roles for easy identification

Now, let's examine the most important aspects to understand the behavior of the field.

1. Stores value as IDs in the post meta table

#link copied

The first important aspect to understand about the User field is where it's stored. The value of the user field is stored in the post meta table as a record related to your post.

While this default approach is flexible, it's not optimized for large queries. If you anticipate querying a significant number of items by this field, it's advisable to consider using the Bidirectional feature from the outset.

The User field stores user IDs in the database regardless of the Return Format setting. If the multiple setting is disabled, it will save a single ID; otherwise, it will save a serialized array of user IDs.

The chosen user is stored as an ID in the post meta value

It's essential to remember that you'll need to master querying by them.

Aside from manual query mastery, you may also consider using the Advanced Views plugin, which introduces smart templates with built-in post queries. The queries feature operates on top of the WP_Query class and saves time by automating routine tasks.

2. Supports the bidirectional feature

#link copied

The ACF User field supports the bidirectional feature, automating the updating of fields on related objects. This eliminates the need for manual queries and simplifies the use of field values, especially for beginners.

With the bidirectional setting enabled for the ACF User field, related fields are automatically kept up-to-date.

Bidirectional setting of the ACF User field
To set up the bidirectional feature, navigate to the Advanced tab

Let's illustrate this with an example involving Company CPTs. A company can have multiple employees (WP users), represented by an 'Employees' User field in the Company CPT. This setup is relatively straightforward.

Now, let's dive into the task at hand. Our goal is to retrieve a list of all companies where the current user is associated. With the introduction of the Bidirectional feature, we can achieve this without creating a custom query. Here's how:

  1. Create a New Post Object Field:
    Add a new Post Object or Relationship field, perhaps named 'Companies', and attach it to the user account's screen.
  2. Enable the Bidirectional feature:
    Navigate to the 'Employees' User field and enable the Bidirectional feature. Set the target field as the new 'Companies' field.

Once these steps are completed, ACF will automatically track changes. When you update the 'Employees' User field, ACF will automatically update the related company in the associated user's 'Companies' field.

This approach ensures that you always obtain an up-to-date list of the companies associated with each user, just like the value of the original Post Object field.

Pro tip: You can further enhance this setup by enabling the Bidirectional feature for the 'Companies' Post Object field as well. This way, any additions or removals of companies on the user's side will be automatically reflected on the company's side too.

In this manner, you can establish bidirectional relations between different items.

Apart from the CPT-to-user case described above, you can also set up a user-to-user relationship. For instance, WordPress users with the 'boss' role may have the 'employees' user field, while WordPress users with the 'employee' role may have the 'boss' user field.

3. Has six different value formats

#link copied

While IDs are stored in the database, the behavior of the get_field function for the User field in code can vary depending on the field settings. The response may be:

  • An array of WP_User instances
  • An array of user IDs
  • An array of user info arrays
  • A single WP_User instance
  • A single-user ID
  • A single array with user info

The 'multiple' setting determines whether the return value will be an array of items or a single item, while the 'Return format' setting dictates the value type. If the 'multiple' setting is enabled, the function will always return an array, regardless of whether the single item option is checked.

The extensive field settings provide flexibility to customize setups according to our needs. However, they can also add complexity to coding tasks, as we need to consistently remember the specific field settings.

To overcome this challenge, consider utilizing smart templates from the Advanced Views plugin.

These templates introduce automated generation and built-in post queries, simplifying tasks such as retrieving field values, converting formats, and passing them to the template. This allows you to focus on designing the layout without being concerned about the details of field settings.

Summary

#link copied

The ACF User field type enables the 'relation' of one or more users to posts and custom post type (CPT) items.

While WordPress posts and CPT items include a built-in author field, the User field is designed to be more versatile.

Unlike the post author field, which is automatically filled by WordPress, the ACF User field is a custom meta field filled by editors. It can store one or more users, providing flexibility for indirect relationships, such as the Employees user field in the Company CPT.

The user field stores its value in the post meta table, which may affect performance when querying by this field. However, this limitation can be overcome by utilizing the bidirectional feature.

The bidirectional feature transforms the User field into a superfield, automating the updating of fields on related objects. This eliminates the need for manual queries and simplifies the use of field values.

The User field value can have six different formats, depending on the field settings.

To simplify working with the fields, you can utilize smart templates from the Advanced Views plugin. These templates introduce automated generation and built-in post queries, allowing you to focus on designing the layout without being concerned about the details of field settings.

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