WPLake > Blog > ACF Taxonomy field: 4 Things You Should Know

ACF Taxonomy field: 4 Things You Should Know

Time to read: 6 mins

-

ACF Plugins Tips and Tricks

-

Updated 21.03.24

Learn how to configure the Taxonomy field to avoid issues with queries and how to turn it into a superfield using the Bidirectional feature.

Table of Contents

Intro

#link copied

Advanced Custom Fields (ACF) is among the top meta field plugins. The Taxonomy field is part of the Relationship group fields, enabling users to select one or more Taxonomy terms. It can be viewed as an extension of the Select field specifically tailored for taxonomy term objects.

The taxonomy fields allow editors to select terms within a single taxonomy only. By utilizing the Appearance setting, you can enable or disable the option to select multiple items simultaneously.

ACF Taxonomy field
The taxonomy field allows editors to choose terms within a single taxonomy only
Editors look of ACF Taxonomy field
The checkbox is the default appearance setting and supports multiple values
Multiselect appearance of the ACF Taxonomy field
The multi-select is another appearance setting that supports multiple values
Radio buttons appearance of the ACF taxonomy field
The radio button setting limits the editor's choice to one item
Select appearance of ACF Taxonomy field
The select setting is another option that limits the editor's choice to one item

Let's review the main aspects of the Taxonomy field to understand its behavior.

1. Uses the post meta table (by default)

#link copied

The most crucial consideration regarding the Taxonomy field is its storage mechanism. By default, like other field types, the Taxonomy field stores the editor's choice in the Postmeta table.

Serialized value of the ACF Taxonomy field in the postmeta table
By default, taxonomy stores a serialized array of term IDs in the post-meta table

In addition, if the taxonomy field is configured to allow multiple choices, the value will be serialized, which can make querying by it difficult.

So the default Taxonomy field behavior differs from the core WordPress taxonomies feature, which utilizes separate tables. Primarily, this storage method affects the querying process. The post meta table is not optimized for queries, whereas taxonomy tables are.

Custom WordPress Taxonomy
WordPress taxonomies
WordPress term relationships table
Unlike meta fields, WordPress taxonomies utilize separate tables optimized for queries

Therefore, if you intend to query posts by the taxonomy field and have many items in your Custom Post Type (CPT), using the Postmeta storage can significantly degrade performance.

However, this doesn't mean we should automatically prefer the native Taxonomies feature and disregard the Taxonomy field. Fortunately, ACF developers have addressed this issue.

The Taxonomy field offers 'Load' and 'Save' terms options. When enabled, the Taxonomy field starts utilizing the WordPress taxonomy tables while retaining the familiar UI for editors and easy code access for us.

ACF Taxonomy field
Enabling the 'save' and 'load' terms options stores values in WordPress taxonomy tables

Tip: We recommend always enabling these settings. While you may not need to query by that taxonomy field right away, it may be necessary later on. If you haven't enabled them initially, migrating tasks can become a hassle.

2. Supports the bidirectional feature

#link copied

Considering the previous chapter, you may wonder why to use the Taxonomy field instead of native WP taxonomies. Although both provide the same functionality, just with a different UI, the reason to prefer ACF Taxonomy may not just be UI preferences.

The ACF Taxonomy field supports the bidirectional feature, allowing for the automatic updating of fields on related objects. This feature eliminates the need for making queries altogether and allows us to use the field value instead. It's much easier, especially for beginners.

Bidirectional setting of the ACF Taxonomy field
The bidirectional feature allows us to automatically keep related fields up-to-date

So how does it work? Suppose we have the Book CPT and Genre Taxonomy. We create a 'genre' taxonomy field and attach it to the Book CPT. Then we create a 'books' relationship field and attach it to the Genre Taxonomy, enabling the bidirectional feature for both, and choosing each other in the settings.

Now the fields are synced, meaning that adding or removing Book CPT items in some Genre taxonomy item will add or remove this taxonomy for the Book CPT terms list.

Isn't it nice? Now we can get a list of Books of the current Genre, or vice versa, without making any queries at all. Check the relationship field article to see how it may work.

Using the bidirectional feature along with the 'load' and 'save' term field settings allows you to create a taxonomy field that's like steroids, making it easier to use than the native WP taxonomies UI.

3. Stores values as IDs

#link copied

It's important to remember that the Taxonomy field always stores chosen terms as IDs, regardless of the Return Format settings or storage type.

So whether you have the default setup that stores values in the post meta table or you've activated 'load' and 'save' options with values stored in the taxonomy tables, in both cases, the value will be written to the database as IDs.

Serialized value of the ACF Taxonomy field in the postmeta table
In the post meta table, even multiple values are saved as IDs
WordPress term relationships table
In the taxonomy tables, every relation has a separate record, and each one uses an ID

4. Has four different value formats

#link copied

While IDs are saved in the storage, in code, the get_field function may give different responses for the Taxonomy field, depending on the field settings. It can be:

  • An array of WP_Term instances
  • An array of term IDs
  • A single WP_Term instance
  • A single term ID

The appearance setting controls whether it will be an array or a single item, while the Return format controls the value type. If you've chosen the appearance setting that supports multiple values, it'll always return an array, even if the single item is checked.

The rich field settings allow us to make subtle setups according to our needs but also make it harder to work in code, as we need to remember the specific field settings all the time.

You can avoid this hassle by using smart templates from the Advanced Views plugin, with automated template generation and built-in post queries. They'll take care of getting field values, converting formats, and passing them to the template, while you can focus on the layout itself.

The plugin supports all the meta fields made by the ACF plugin, along with other data vendors.

Summary

#link copied

The ACF Taxonomy field can serve as an alternative input to the native WordPress taxonomy's sidebar.

By default, the Taxonomy field settings store values in the post meta table, which can have drawbacks when it comes to querying. It's highly recommended to activate the 'load' and 'save' terms options to ensure the best performance and avoid any potential issues with querying in the future.

With the bidirectional feature enabled, the Taxonomy field becomes a superfield, always up-to-date and synced with the related field.

Regardless of the storage setting, the values are saved as IDs. However, the response of the get_field function depends on the field settings and offers four options.

To streamline your workflow and avoid any formatting hassles, you can utilize the Advanced Views plugin, which introduces smart templates. This allows you to focus solely on the layout itself.

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