Issue on Getting Custom Field Value Added To the Custom Taxonomy

enter image description here

I am able to add a Cutom Field to The Custom Taxonomy and save it using the following code. I checked the database and the result is saving into the wp_options table like

and when I try to get the taxonomy terms I am only getting the built in values like name, slug and …

array(2) { [0]=> object(WP_Term)#4243 (10) { [“term_id”]=> int(7) [“name”]=> string(2) “BB” [“slug”]=> string(2) “bb” [“term_group”]=> int(0) [“term_taxonomy_id”]=> int(7) [“taxonomy”]=> string(6) “genres” [“description”]=> string(0) “” [“parent”]=> int(0) [“count”]=> int(0) [“filter”]=> string(3) “raw” } 1=> object(WP_Term)#4242 (10) { [“term_id”]=> int(6) [“name”]=> string(2) “ZZ” [“slug”]=> string(2) “zz” [“term_group”]=> int(0) [“term_taxonomy_id”]=> int(6) [“taxonomy”]=> string(6) “genres” [“description”]=> string(0) “” [“parent”]=> int(0) [“count”]=> int(0) [“filter”]=> string(3) “raw” } }

but no Custom Field Value! Can you please let me know how I can access it and display it on the page?

 function mj_taxonomy_edit_custom_meta_field($term) {

        $t_id = $term->term_id;
        $term_meta = get_option( "taxonomy_$t_id" ); 
       ?>
        <tr class="form-field">
        <th scope="row" valign="top"><label for="term_meta[class_term_meta]"><?php _e( 'Dad Name', 'MJ' ); ?></label></th>
            <td>
                <input type="text" name="term_meta[class_term_meta]" id="term_meta[class_term_meta]" value="<?php echo esc_attr( $term_meta['class_term_meta'] ) ? esc_attr( $term_meta['class_term_meta'] ) : ''; ?>">
                <p class="description"><?php _e( 'Enter a value for this field','MJ' ); ?></p>
            </td>
        </tr>
    <?php
    }
add_action( 'genres_add_form_fields','mj_taxonomy_edit_custom_meta_field', 10, 2 );
add_action( 'genres_edit_form_fields','mj_taxonomy_edit_custom_meta_field', 10, 2 );


function mj_save_taxonomy_custom_meta_field( $term_id ) {
        if ( isset( $_POST['term_meta'] ) ) {

            $t_id = $term_id;
            $term_meta = get_option( "taxonomy_$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_$t_id", $term_meta );
        }

    }  
add_action( 'edited_genres', 'mj_save_taxonomy_custom_meta_field', 10, 2 );  
add_action( 'create_genres', 'mj_save_taxonomy_custom_meta_field', 10, 2 );

Read more here:: Issue on Getting Custom Field Value Added To the Custom Taxonomy

Leave a Reply

Your email address will not be published. Required fields are marked *