Create Taxonomy Term Meta in WordPress

Just like post metadata, the WordPress term meta is additional information stored against a taxonomy term. A single entry in a taxonomy is called a term. This post demonstrates how to add term meta to a custom taxonomy. We will create a term meta "zipcode" for our property-city taxonomy.

Create Taxonomy Term Meta in Wordpress

WordPress not only allows us to add custom post types and custom taxonomies but also provides different hooks to extend term form fields. We will use four WordPress action hooks to save term meta for a taxonomy term.

  1. $taxonomy_add_form_fields hook is used to add term meta form fields to the new term form.
  2. $taxonomy_edit_form_fields hook is used to add term meta form fields to the edit term form.
  3. created_$taxonomy hook saves term meta when a new term is created.
  4. edited_$taxonomy hook saves term meta when an existing term is edited or updated.

WordPress hooks can be used like this:

add_action( "$taxonomy_add_form_fields", "function_to_show_fields", 10, 1 );
add_action( "$taxonomy_edit_form_fields", "function_to_show_fields", 10, 2 );
add_action( "created_$taxonomy", "funtion_to_save_fields", 10, 2 );
add_action( "edited_$taxonomy", "funtion_to_save_fields", 10, 2 );

Where $taxonomy is the slug of taxonomy in this post, it's going to be "city". You need to change it to your taxonomy slug for which you are going to create the custom term meta form fields. We will create a separate file terms-meta.php, and then include this file in the functions.php file. We will add the mentioned action hooks in our terms-meta.php file to add term form fields. Visit our post Add Custom Column to Taxonomies Table to learn how to show the term meta column in the taxonomies table.

 

How to Add Term Meta Fields to Term Add & Edit Form

We are going to implement it in one file since the implementation is simple enough. Steps to add term form fields to the term form.

  • Add action hook $taxonomy_add_form_fields to add term meta field to the term add form.
  • Add action hook $taxonomy_edit_form_fields to add term meta field to the term edit form.
  • Add action hook created_$taxonomy and edited_$taxonomy to save term meta when a term is either created or updated.

terms-meta.php

<?php
// Hook to show term field on add term form
add_action('property-city_add_form_fields', 'add_property_city_meta_fields', 10, 2);
function add_property_city_meta_fields(): void
{ ?>
<div class="form-field term-group selectdiv">
<label class="title-label">
<?php _e('Zipcode', 'text-domain'); ?>
<span>*</span>
</label>
<input type="number" name="zipcode" class="regular-text" required="required"/>
<p class="description"><?php _e('Enter zipcode of city', 'text-domain'); ?></p>
</div>
<?php
}

// Hook to show term fields on edit taxonomy form
add_action('property-city_edit_form_fields', 'taxonomy_property_city_edit_meta_fields', 10, 2);
function taxonomy_property_city_edit_meta_fields($term, $taxonomy): void
{
$zipcode = get_term_meta($term->term_id, 'zipcode', true); ?>
<tr class="form-field">
<th scope="row" valign="top">
<?php _e('Zipcode', 'text-domain'); ?>
<span>*</span>
</th>
<td>
<div class="property-city-metabox">
<input type="number" name="zipcode" class="regular-text" value="<?= $zipcode; ?>" required="required"/>
<p class="description"><?php _e('Enter zipcode of city', 'text-domain') ?></p>
</div>
</td>
</tr>
<?php
}

// Two hooks with same function to save term meta when term is created/edited
add_action('created_property-city', 'save_taxonomy_property_city_meta', 10, 2);
add_action('edited_property-city', 'edit_property_city_meta', 10, 2);
function edit_property_city_meta($term_id): void
{
if (isset($_POST['zipcode'])) {
update_term_meta($term_id, 'zipcode', sanitize_meta($_POST['zipcode']));
}
}

All done, now all we need to do is include terms-meta.php in our theme's functions.php file.
<?php include('terms-meta.php');?>

You can use the get_term_meta() function to fetch the term meta of a single term:
get_term_meta($term_id, 'meta_key', true);

Where $term_id is the single term id, meta_key is the field name we saved, which was "city" used in this post, and the last parameter is used for response; if set to true, then it will return a single value.

Taxonomy Term Meta

We just added taxonomy term meta and now have a working term meta field in our taxonomy's add and edit form that can be used in taxonomy related queries and templates.