0

I have a custom taxonomy with custom fields which are being populated with CMB2. I can't seem to figure out why I am unable to populate this information on the frontend.

Below is the code that is generating the custom fields on my custom taxonomy called Placement.

     add_action( 'cmb2_admin_init', 'placement_register_taxonomy_metabox' ); 
 function placement_register_taxonomy_metabox() { 
    $prefix = 'placement_'; 
    $cmb_term = new_cmb2_box( array( 
        'id'               => $prefix . 'placement', 
        'title'            => esc_html__( 'Title Handler', 'veruscref-theme' ), // Doesn't output for term boxes 
        'object_types'     => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta 
        'taxonomies'       => array( 'placement' ), // Tells CMB2 which taxonomies should have these fields 
        // 'new_term_section' => true, // Will display in the "Add New Category" section 
    ) ); 
 $cmb_term->add_field( array( 
        'name'     => esc_html__( 'Loan Program Title', 'veruscref-theme' ), 
        'desc'     => esc_html__( 'Will be displayed on transaction page', 'veruscref-theme' ), 
        'id'       => $prefix . 'tax_header', 
        'type'     => 'title', 
        'on_front' => false, 
    ) ); 

    $cmb_term->add_field( array( 
        'name' => esc_html__( 'Title', 'veruscref-theme' ), 
        'id'   => $prefix . 'tax_title', 
        'type' => 'text', 
    ) ); 
     $cmb_term->add_field( array( 
        'name' => esc_html__( 'Small Title', 'veruscref-theme' ), 
        'id'   => $prefix . 'tax_small_title', 
        'type' => 'text', 
    ) ); 

Below is my frontend code

<?php
$terms = get_terms(
array(
    'taxonomy'   => 'placement',
    'hide_empty' => 1,
    'exclude' => array(4,23),
)
);
if ( ! empty( $terms ) && is_array( $terms ) ) { foreach ( $terms as $term ) { ?>       
<?php                        
$title = get_term_meta( get_queried_object_id(), 'placement_tax_title', true );
echo $title;                                             
?>  
<?php } } ?>

What am I doing wrong with my code?

6
  • I think get_queried_object_id() will only return a term's ID if you're looking at a term archive - is that what you intend?
    – bosco
    Commented Jun 29, 2021 at 19:39
  • 1
    I don't see any metabox API calls in your code, only CMB2 API calls, you need to ask this in a CMB2 community instead, or at their support routes, 3rd party plugin dev support is off topic here. Also, why are you using get_queried_object_id instead of $term->ID?
    – Tom J Nowell
    Commented Jun 29, 2021 at 19:42
  • @TomJNowell I apologize if my question was off topic. As for $term->ID I have tried that and it produced a fatal error on the page Commented Jun 29, 2021 at 19:48
  • 1
    it may be term_id, I'm unsure of the exact name but the $term object contains the ID of the term
    – Tom J Nowell
    Commented Jun 29, 2021 at 20:01
  • Thank you @TomJNowell it turned out to be get_term_meta( $term->term_id) and that did the trick. Thank you Commented Jun 29, 2021 at 20:07

1 Answer 1

0

You need to use $term->term_id not get_queried_object_id() in get_term_meta

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.