Understanding and Troubleshooting CMB2 Taxonomy Field Issues
Content
If you're using the CMB2 plugin to manage custom fields in WordPress, you've likely encountered its powerful taxonomy field types, such as taxonomy_select and taxonomy_multicheck. These fields are designed to replace the default WordPress taxonomy metaboxes, allowing for a more customized admin experience. However, their unique behavior often leads to confusion, especially around how data is stored and retrieved.
Based on common support threads, this guide will help you understand why these issues occur and how to resolve them.
Common Problem 1: Data is Not Saving or Retrieving Correctly
Why it happens: This is the most frequent point of confusion. The CMB2 taxonomy fields (taxonomy_select, taxonomy_multicheck, etc.) do not store their values as post meta or user meta. Instead, they function as an alternative interface for assigning WordPress terms to a post or user object. When you select a term, you are creating a standard term relationship, not saving metadata.
How to fix it: To retrieve the terms assigned via a CMB2 taxonomy field, you must use standard WordPress term functions, not get_post_meta() or get_user_meta().
- For Posts: Use
get_the_terms( $post_id, 'your_taxonomy_slug' ). - For Users: Use
wp_get_object_terms( $user_id, 'your_taxonomy_slug' ).
Common Problem 2: Needing a Hierarchical or Conditional Display
Why it happens: The default taxonomy fields display all terms in a flat list, which can be cumbersome for large, hierarchical taxonomies (like categories with many sub-items). Users often want to show child terms only after a parent term has been selected.
How to fix it: The core CMB2 plugin does not include conditional logic. To achieve this, you have a few options:
- Use a Helper Plugin: The
CMB2 Conditionalsplugin (found on GitHub) can be used to show/hide fields based on the value of another field. - Use a Different Field Type: Consider using a standard
selectormulticheckfield and populate it with terms using a callback function. The CMB2 documentation provides a helpful snippet for this. This method gives you full control over which terms are displayed and how. - Use Select2: Add-ons like
CMB2 Field Type: Select2can improve the user experience for large lists but still require you to handle the term data population manually if you want to avoid the default term assignment behavior.
Common Problem 3: Taxonomy Field is Not Repeatable
Why it happens: The taxonomy_select and taxonomy_radio field types are explicitly noted in the CMB2 documentation as not being available as repeatable fields within a group. This is a technical limitation of how these specific field types are built.
How to fix it: If you need to select multiple terms in a repeatable group, you must use a different field type, such as a standard select field populated with term options via a callback function.
Common Problem 4: 'Invalid Taxonomy' Error
Why it happens: This error typically means one of two things: the taxonomy slug you provided does not exist, or the code is running on a hook that is too early, before the taxonomy (especially from another plugin like WooCommerce) has been registered.
How to fix it:
- Double-check that the 'taxonomy' parameter in your field array uses the correct slug (e.g.,
'product_cat', not'product category'). - Ensure your CMB2 metabox registration code is hooked to a later action like
cmb2_admin_initorinitwith a sufficient priority to ensure the custom taxonomy is available.
By understanding that CMB2 taxonomy fields manage term relationships directly, you can avoid the common pitfall of searching for meta values and instead use the correct WordPress functions to retrieve your data. For more complex UI needs, combining CMB2 with helper plugins or custom callback functions provides a powerful and flexible solution.
Related Support Threads Support
-
Taxonomy child elements hidden while parent is not selectedhttps://wordpress.org/support/topic/taxonomy-child-elements-hidden-while-parent-is-not-selected/
-
show subtaxonomy when parent taxonomy is selectedhttps://wordpress.org/support/topic/show-subtaxonomy-when-parent-taxonomy-is-selected/
-
show parent taxonomy onlyhttps://wordpress.org/support/topic/show-parent-taxonomy-only/
-
taxonomy_multicheck_inline field output “Invalid taxonomy.”https://wordpress.org/support/topic/taxonomy_multicheck_inline-field-output-invalid-taxonomy/
-
Get categorieshttps://wordpress.org/support/topic/get-categories-2/
-
Can't access Taxonomy_multicheck data for userhttps://wordpress.org/support/topic/cant-access-taxonomy_multicheck-data-for-user/
-
Default value 'none' for taxonomy_select and taxonomy_multicheckhttps://wordpress.org/support/topic/default-value-none-for-taxonomy_select-and-taxonomy_multicheck/
-
Repeating Group Field with Taxonomy Selecthttps://wordpress.org/support/topic/repeating-group-field-with-taxonomy-select/
-
Conditional Select Fieldhttps://wordpress.org/support/topic/conditional-select-field/
-
taxonomy select variable format in DBhttps://wordpress.org/support/topic/taxonomy-select-variable-format-in-db/
-
A dropdown for taxonomy termshttps://wordpress.org/support/topic/a-dropdown-for-taxonomy-terms/
-
CMB in taxonomyhttps://wordpress.org/support/topic/cmb-in-taxonomy/
-
Select2 for Taxonomy Select fieldhttps://wordpress.org/support/topic/select2-for-taxonomy-select-field/
-
Taxonomy Select Customhttps://wordpress.org/support/topic/taxonomy-select-custom/
-
Field type: Taxonomy selecthttps://wordpress.org/support/topic/field-type-taxonomy-select/
-
CMB2 Taxonomy Select not saving in databasehttps://wordpress.org/support/topic/cmb2-taxonomy-select-not-saving-in-database-1/
-
Taxonomy field (ACF) to taxonomy_multiselect (CMB2)https://wordpress.org/support/topic/taxonomy-field-acf-to-taxonomy_multiselect-cmb2/