Home Support General HivePress How to Add Custom Fields to Custom Taxonomies

How to Add Custom Fields to Custom Taxonomies

  • Author
  • nialb

    Using the HivePress demo data as the example, I want to extend the existing custom taxonomy ‘hp_listing_category’ by adding a new custom field of say ‘hp_category_excerpt’, to it.

    This will allow keeping the current short ‘Description’ but will add a new longer more verbose ‘Excerpt’ to each and every Listing Category in the HivePress plugin, which can then be used throughout the ListingHive theme (Descriptions in Blocks and the Excerpts on Pages).

    I have a working php snippet BUT it is not elegant and does NOT harness the modular code layout used in the plugin and theme.

    // A callback function to add a custom field to our "presenters" taxonomy  
    function presenters_taxonomy_custom_fields($tag) {  
       // Check for existing taxonomy meta for the term you're editing  
        $t_id = $tag->term_id; // Get the ID of the term you're editing  
        $term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check  
    <tr class="form-field">  
        <th scope="row" valign="top">  
            <label for="presenter_id"><?php _e('WordPress User ID'); ?></label>  
            <input type="text" name="term_meta[presenter_id]" id="term_meta[presenter_id]" size="25" style="width:60%;" value="<?php echo $term_meta['presenter_id'] ? $term_meta['presenter_id'] : ''; ?>"><br />  
            <span class="description"><?php _e('The Presenter\'s WordPress User ID'); ?></span>  
    // A callback function to save our extra taxonomy field(s)  
    function save_taxonomy_custom_fields( $term_id ) {  
        if ( isset( $_POST['term_meta'] ) ) {  
            $t_id = $term_id;  
            $term_meta = get_option( "taxonomy_term_$t_id" );  
            $cat_keys = array_keys( $_POST['term_meta'] );  
                foreach ( $cat_keys as $key ){  
                if ( isset( $_POST['term_meta'][$key] ) ){  
                    $term_meta[$key] = $_POST['term_meta'][$key];  
            //save the option array  
            update_option( "taxonomy_term_$t_id", $term_meta );  
    // Add the fields to the "presenters" taxonomy, using our callback function  
    add_action( 'hp_listing_category_edit_form_fields', 'presenters_taxonomy_custom_fields', 10, 2 );  
    // Save the changes made on the "presenters" taxonomy, using our callback function  
    add_action( 'edited_hp_listing_category', 'save_taxonomy_custom_fields', 10, 2 ); 

    If anyone has a better snippet or knows what direction to point me in, I’d appreciate your input.


    ihor developer

    You can declare a new field via the “hivepress/v1/meta_boxes” filter (check how existing category fields are declared here https://github.com/hivepress/hivepress/blob/master/includes/configs/meta-boxes.php) – it’ll automatically appear in the listing category form and will be validated/saved as a meta field. Then you can use it in templates.


    Thanks @ihor,

    I added the following snippet and it works as expected, thanks again.

    	function($config ) {
    		// Add excerpt text area
    		$config['listing_category_settings']['fields']['cat_excerpt'] = [			                
    				'label'		  => esc_html__( 'Excerpt', 'hivepress' ),
    				'description' => esc_html__( 'The Excerpt is the longer version of the Description above and will be used on the individual Category Pages', 'hivepress' ),
    				'type'        => 'textarea',
    				'max_length'  => 2048,
    				'default'     => '',
    				'html'        => true,
    				'_order'      => 5,
    		return $config;

    Now I wish to use that new meta field of cat_excerpt in the template called listing-category-header.php but cannot get the logic correct.

    I have

    <?php if ( ***excerpt exists*** ) : ?>
    				<div class="hp-listing-category__description"><?php echo ***excerpt*** ; ?></div>
    			<?php else : ?>
    				<div class="hp-listing-category__description"><?php echo esc_html( $listing_category->get_description() ); ?></div>
    			<?php endif; ?>

    My issue is, I don’t understand what array to query for the existence of this new meta field?

    As always @ihor, your guidance is much appreciated.

    ihor developer

    In this case it’ll be stored as a meta field so it should be available via:

    $cat_excerpt=get_term_meta($category_id, 'hp_cat_excerpt', true);


    Thanks @ihor, works perfectly (with a subtle change).

    I created a child theme and edited the template ‘listing-category-header.php’ directly as follows:

    <?php	if ( get_term_meta ( get_queried_object_id(), 'hp_cat_excerpt', true) ) : ?>
    				<div class="hp-listing-category__description"><?php echo get_term_meta (get_queried_object_id(), 'hp_cat_excerpt', true) ; ?></div>
    			<?php else : ?>
    				<div class="hp-listing-category__description"><?php echo esc_html( $listing_category->get_description() ); ?></div>
    			<?php endif; ?>

    You can mark this thread -solved-.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘How to Add Custom Fields to Custom Taxonomies’ is closed to new replies.