ACF Taxonomy Field
Key Points at a Glance
- Storage Mechanism: By default, the Taxonomy field stores data in the Postmeta table, differing from native WordPress taxonomies which use separate tables optimized for queries.
- Bidirectional Feature: The ACF Taxonomy field supports bidirectional syncing, automatically updating related fields on associated objects, simplifying content management.
- Value Storage: Taxonomy field stores chosen terms as IDs, regardless of Return Format settings or storage type, ensuring consistency in data representation.
- Optimizing Performance: Activating 'load' and 'save' terms options enhances performance and query efficiency, recommended for smoother operations and future-proofing.
Table of Contents
Intro
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.
Let's review the main aspects of the Taxonomy field to understand its behavior.
1. Uses the post meta table (by default)
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.
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.
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.
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
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.
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
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.
4. Has four different value formats
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:
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
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.
Thank you for reading! Subscribe to our monthly newsletter to stay updated on the latest WordPress news and useful tips.
Totally useless
Slightly helpful
Very helpful
Frequently Asked Questions Test Your Knowledge
FAQ mode
/
Learning mode
- What is the main difference between the Taxonomy field and native WordPress taxonomies?
The main difference lies in their storage mechanism; the Taxonomy field stores data in the Postmeta table by default, while native WordPress taxonomies use separate tables optimized for queries.
- How can I ensure optimal performance when using the Taxonomy field?
It's recommended to activate the 'load' and 'save' terms options to store values in WordPress taxonomy tables, improving query efficiency and avoiding potential issues with querying in the future.
- What advantage does the bidirectional feature of the Taxonomy field offer?
The bidirectional feature allows for automatic updating of related fields on associated objects, simplifying content management and ensuring data consistency.
- What tools can I use to streamline workflow and data handling with the Taxonomy field?
Integrating the Advanced Views plugin automates template generation and post queries, allowing developers to focus on layout design while the plugin handles data retrieval and formatting.