How to Add Custom Meta Fields to Custom Taxonomies in WordPress (2023)
If you‘ve ever wanted to attach additional data fields to your WordPress taxonomies, you‘re in the right place. Custom taxonomies provide a powerful way to organize content beyond the default categories and tags. Adding custom meta fields to your taxonomies allows you to include even more useful information.
In this in-depth guide, we‘ll walk through why you might want to use custom meta fields and provide step-by-step instructions for adding them to your custom taxonomies using a plugin. We‘ll also cover how to display the custom fields on your taxonomy archive pages.
Let‘s get started!
What are Custom Taxonomies?
Before we dive into adding custom meta fields, let‘s quickly review what custom taxonomies are and why you might want to use them on your WordPress site.
Essentially, a taxonomy is a way to group similar pieces of content together. WordPress comes with two default taxonomies – Categories and Tags. You can assign blog posts to specific categories or tags to organize them based on topic.
Custom taxonomies take this a step further by allowing you to create your own groupings that are separate from categories and tags. Some examples of custom taxonomies you might find on different types of websites:
- A food blog with taxonomies for Cuisine, Ingredients, or Courses
- A movie review site with taxonomies for Genres, Actors, or Directors
- An e-commerce store with taxonomies for Product Types, Brands, or Colors
- A real estate website with taxonomies for Property Types, Locations, or Amenities
You can create custom taxonomies to group any type of content in a way that makes sense for your particular niche or industry. To learn more, check out our complete guide to creating custom taxonomies in WordPress.
Why Use Custom Meta Fields?
By default, taxonomies only include a few basic fields like the name, slug, description, and parent term. But often there is other related information you may want to attach.
This is where custom meta fields come into play. Custom fields allow you to add extra data to your taxonomies that doesn‘t fit into the default fields.
Here are a few examples of custom meta fields you might want to add to different taxonomies:
- Actors taxonomy: date of birth, photo, bio, website link
- Property Types: icon, description
- Ingredients: photo, description, nutrition facts
- Product Colors: color hex code, photo
The possibilities are endless! Custom meta fields provide flexibility to include whatever additional data is relevant to describing and organizing your taxonomies.
Paired with custom taxonomies, custom fields allow you to transform WordPress into a more powerful content management system capable of structuring all sorts of data.
Now that you know the why, let‘s look at the how! We‘ll use a plugin to add custom meta fields to our taxonomies.
How to Add Custom Meta Fields to Taxonomies
For this tutorial, we‘ll be using the Advanced Custom Fields (ACF) plugin. It‘s a popular free plugin that makes it easy to add custom fields to various areas of your WordPress site, including taxonomies.
If you‘re using a different custom fields plugin, the specific steps may vary but the general principles will be the same.
Step 1: Install Advanced Custom Fields Plugin
First, you‘ll need to install and activate the free version of the ACF plugin. If you need help, check out our step-by-step guide to installing a WordPress plugin.
Step 2: Create a New Field Group
With the ACF plugin activated, go to Custom Fields ᐳ Add New. You‘ll see an interface to create a Field Group, which is essentially a set of custom fields.
Give your Field Group a name. Since we‘ll be adding this to a taxonomy, use a descriptive name like "Actor Details". This name will appear on the taxonomy edit screen.
Step 3: Add Custom Fields
Click the + Add Field button to create a new field.
Enter a Label for the field, and the Field Name will automatically populate. You‘ll use the Field Name to display this data on the frontend, so make note of it.
Select the type of field using the Field Type dropdown. ACF supports many field types including text, textarea, number, image, file, select, checkbox, and more. Choose the type that matches the data you intend to store.
Fill out any other setting for the fields like instructions, placeholder text, default value, etc. depending on the Field Type you selected.
Here are some example fields we might add for our Actor taxonomy:
- Birth Date (date picker field type)
- Photo (image field type)
- Biography (textarea field type)
- Website (url field type)
Repeat this process by clicking the +Add Field button if you want to add multiple custom fields to the taxonomy.
Step 4: Configure Location Rules
Scroll down to the Location settings. This is where you set the rules for where these custom fields will appear.
Configure the rules like this:
- Show this field group if Taxonomy is equal to Actor
Replace "Actor" with the name of your custom taxonomy. If you want the fields to appear on multiple taxonomies you can add additional rules.
Step 5: Publish the Field Group
With your custom fields set up, click the blue Publish button to save the Field Group.
Step 6: Add Data to Custom Fields
Now when you edit a term in your custom taxonomy, you‘ll see the custom fields appear:
Enter the relevant data for each field and update the term. Repeat this for each of your terms.
Displaying Custom Meta Fields on the Frontend
So you‘ve added custom fields to your taxonomies in the WordPress admin. But your site visitors can‘t see this data yet. The final step is to display the custom fields on the frontend of your site.
We‘ll do this by adding some code to the taxonomy template file. You‘ll need to be comfortable editing your WordPress theme files.
Go to Appearance ᐳ Theme Editor in your WordPress admin. If you don‘t see the Theme Editor, you may need to enable it.
Look for a file called taxonomy-{taxonomyname}.php. So if your taxonomy is called "actor" look for taxonomy-actor.php. If this file doesn‘t exist, WordPress will fall back to using archive.php or index.php as the template for your taxonomy archive.
In your taxonomy template file, find the area that displays the term name, description, or other meta information. This will vary based on your theme. You may see WordPress template tags like the_archive_title() or the_archive_description().
Paste in the following code snippet in the appropriate spot to display a custom field:
echo get_field(‘field_name‘, get_queried_object());Make sure to replace field_name with the actual Field Name of your custom field (the one you noted down in Step 3).
Here‘s a more complete example of what this might look like in your template file:
<p><?php echo term_description(); ?></p>
<ul class="actor-details">
<li>
<strong>Birth Date:</strong>
<?php echo get_field(‘birth_date‘, get_queried_object()); ?>
</li>
<li>
<strong>Biography:</strong>
<?php echo get_field(‘biography‘, get_queried_object()); ?>
</li>
<li>
<strong>Website:</strong>
<a href="<?php echo get_field(‘website‘, get_queried_object()); ?>">
<?php echo get_field(‘website‘, get_queried_object()); ?>
</a>
</li>
</ul>This displays the term name and description, then outputs the birth_date, biography, and website custom fields in a list. The get_queried_object() function returns the current taxonomy term being viewed.
Style the output of the custom fields by wrapping them in HTML tags and using CSS. How you choose to display them will depend on the design of your theme and the specific data in each field.
Other Methods for Adding Custom Meta Fields
While we used the Advanced Custom Fields plugin for this tutorial, there are other methods and plugins for adding custom fields to taxonomies, each with their own advantages:
- Manually adding fields using the WP Termmeta table and add_term_meta()/update_term_meta() functions
- Using the native custom fields feature for taxonomies added in WordPress 4.4 with the "show_in_rest" argument
- Other custom field plugins like Pods, Toolset, or MetaBox
ACF provides an intuitive interface, but if you‘re a developer you may prefer a code-based approach for more control and customization options. Plugins can be easier to maintain across multiple sites.
Best Practices for Working with Custom Taxonomy Fields
To wrap up, here are some tips and best practices to keep in mind when extending your taxonomies with custom fields:
- Only add custom fields when necessary. Don‘t bloat your taxonomies with extraneous data.
- Be selective with field types. Choose the appropriate type based on the data to maintain database efficiency.
- Sanitize and validate user input. This is critical for security and data integrity, especially if fields will be user-editable.
- Use a naming convention for field names. Consistency makes fields easier to identify and maintain if you have a lot of them.
- Delete unused fields. If you no longer need a field, delete it from the taxonomy to keep the admin experience clean.
- Consider the front-end display. Put thought into how you output custom fields so it provides a good user experience for your visitors.
Custom meta fields bring a lot of flexibility to taxonomies and allow you to tailor them to fit the needs of your particular site and content.
We hope this in-depth guide gives you the knowledge and tools to start leveraging custom meta fields on your own taxonomies!
