Support » Plugin: CMB2 » Retrieve custom taxonomy meta data in custom post type

  • Resolved Phil

    (@outbavk)


    Hello friends, I need some help!

    I have created a custom post type ‘hotel.’

    Each hotel has several amenities like hot tube, room service, etc. I created a custom taxonomy called ‘amenities’ ( aka category) to assign amenities to the custom post type ‘hotel.’

    Each amenity has a name, description, and image (an icon)*.

    I used CMB2 to create the custom meta box (type : file) for the icon input in the amenities custom taxonomy.

    So far it looks good, I created the custom archives page for amenities and it went smoothly.

    But, then I stumbled upon a wall: I created a custom single page for ‘hotel’. but, I cannot find a way to get the icon meta data from each assigned ‘amenity’ to display on the hotel single page front-end. I have spent a good deal of the day trying to find a way; but, I realized I need help. I have added the code below:

    Custom Post Type Hotel Code:

    
    // Register Custom Post Type
    function hotel() {
    
    	$labels = array(
    		'name'                  => _x( 'Hotels', 'Post Type General Name', 'phil_domain' ),
    		'singular_name'         => _x( 'Hotel', 'Post Type Singular Name', 'phil_domain' ),
    		'menu_name'             => __( 'Post Types', 'phil_domain' ),
    		'name_admin_bar'        => __( 'Post Type', 'phil_domain' ),
    		'archives'              => __( 'Item Archives', 'phil_domain' ),
    		'attributes'            => __( 'Item Attributes', 'phil_domain' ),
    		'parent_item_colon'     => __( 'Parent Item:', 'phil_domain' ),
    		'all_items'             => __( 'All Items', 'phil_domain' ),
    		'add_new_item'          => __( 'Add New Item', 'phil_domain' ),
    		'add_new'               => __( 'Add New', 'phil_domain' ),
    		'new_item'              => __( 'New Item', 'phil_domain' ),
    		'edit_item'             => __( 'Edit Item', 'phil_domain' ),
    		'update_item'           => __( 'Update Item', 'phil_domain' ),
    		'view_item'             => __( 'View Item', 'phil_domain' ),
    		'view_items'            => __( 'View Items', 'phil_domain' ),
    		'search_items'          => __( 'Search Item', 'phil_domain' ),
    		'not_found'             => __( 'Not found', 'phil_domain' ),
    		'not_found_in_trash'    => __( 'Not found in Trash', 'phil_domain' ),
    		'featured_image'        => __( 'Featured Image', 'phil_domain' ),
    		'set_featured_image'    => __( 'Set featured image', 'phil_domain' ),
    		'remove_featured_image' => __( 'Remove featured image', 'phil_domain' ),
    		'use_featured_image'    => __( 'Use as featured image', 'phil_domain' ),
    		'insert_into_item'      => __( 'Insert into item', 'phil_domain' ),
    		'uploaded_to_this_item' => __( 'Uploaded to this item', 'phil_domain' ),
    		'items_list'            => __( 'Items list', 'phil_domain' ),
    		'items_list_navigation' => __( 'Items list navigation', 'phil_domain' ),
    		'filter_items_list'     => __( 'Filter items list', 'phil_domain' ),
    	);
    	$args = array(
    		'label'                 => __( 'Hotel', 'phil_domain' ),
    		'labels'                => $labels,
    		'supports'              => array( 'title', 'editor' ),
    		'hierarchical'          => false,
    		'public'                => true,
    		'show_ui'               => true,
    		'show_in_menu'          => true,
    		'menu_position'         => 5,
    		'show_in_admin_bar'     => true,
    		'show_in_nav_menus'     => true,
    		'can_export'            => true,
    		'has_archive'           => true,
    		'exclude_from_search'   => false,
    		'publicly_queryable'    => true,
    		'capability_type'       => 'post',
    	);
    	register_post_type( 'hotel', $args );
    
    }
    add_action( 'init', 'hotel', 0 );
    
    


    Custom Post Type Amenities Code

     
    // Register Custom Taxonomy
    function amenity() {
    
    	$labels = array(
    		'name'                       => _x( 'Amenities', 'Taxonomy General Name', 'phil_domain' ),
    		'singular_name'              => _x( 'Amenity', 'Taxonomy Singular Name', 'phil_domain' ),
    		'menu_name'                  => __( 'Taxonomy', 'phil_domain' ),
    		'all_items'                  => __( 'All Items', 'phil_domain' ),
    		'parent_item'                => __( 'Parent Item', 'phil_domain' ),
    		'parent_item_colon'          => __( 'Parent Item:', 'phil_domain' ),
    		'new_item_name'              => __( 'New Item Name', 'phil_domain' ),
    		'add_new_item'               => __( 'Add New Item', 'phil_domain' ),
    		'edit_item'                  => __( 'Edit Item', 'phil_domain' ),
    		'update_item'                => __( 'Update Item', 'phil_domain' ),
    		'view_item'                  => __( 'View Item', 'phil_domain' ),
    		'separate_items_with_commas' => __( 'Separate items with commas', 'phil_domain' ),
    		'add_or_remove_items'        => __( 'Add or remove items', 'phil_domain' ),
    		'choose_from_most_used'      => __( 'Choose from the most used', 'phil_domain' ),
    		'popular_items'              => __( 'Popular Items', 'phil_domain' ),
    		'search_items'               => __( 'Search Items', 'phil_domain' ),
    		'not_found'                  => __( 'Not Found', 'phil_domain' ),
    		'no_terms'                   => __( 'No items', 'phil_domain' ),
    		'items_list'                 => __( 'Items list', 'phil_domain' ),
    		'items_list_navigation'      => __( 'Items list navigation', 'phil_domain' ),
    	);
    	$args = array(
    		'labels'                     => $labels,
    		'hierarchical'               => true,
    		'public'                     => true,
    		'show_ui'                    => true,
    		'show_admin_column'          => true,
    		'show_in_nav_menus'          => true,
    		'show_tagcloud'              => true,
    	);
    	register_taxonomy( 'amenity', array( 'hotel' ), $args );
    
    }
    add_action( 'init', 'amenity', 0 );
    


    Metabox code

    
    add_action( 'cmb2_init', 'cmb2_add_metabox' );
    function cmb2_add_metabox() {
    
    	$prefix = '_cmb_';
    
    	$cmb = new_cmb2_box( array(
    		'id'           => $prefix . 'icon',
    		'title'        => __( 'Amenity Icon', 'cmb2' ),
    		'object_types' => array( 'amenity' ),
    		'context'      => 'normal',
    		'priority'     => 'default',
    	) );
    
    	$cmb->add_field( array(
    		'id' => $prefix . 'icon',
    		'type' => 'file',
    	) );
    
    }
    

    Loop I tried to use on single-hotel.php but only retrieves default WordPress meta from amenities like name, description, and slug. I cannot find a way to get the meta data from cmb2 taxonomy ‘amenities’ on the custom post type ‘hotel’ single page.

    
    <?php	
    	$terms = get_terms('amenity'); 
    	$icon = get_term_meta(  get_queried_object_id(), '_cmb_icon', true );
    	
    	foreach ( $terms as $term ) {
    	
    		$name = $term->name;
    		$desc = $term->description;
    		
    		echo $name;
    		echo $desc;
    		echo $icon;
    			
    	}	
    ?>
    
    

    Thanks in advance, I appreciate your time and effort!

    • This topic was modified 2 years, 9 months ago by Phil.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Phil

    (@outbavk)

    Edit Note: On the cmb2 code I misspelled a part I meant get_terms(‘amenity’). I updated the comment.

    • This reply was modified 2 years, 9 months ago by Phil.
    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Looks like you have possibly two situations.

    1. Trying to use the post ID to fetch term meta when you need the term ID instead.
    2. Trying to use get_term_meta() to fetch post meta, and you’d want to instead use get_post_meta()

    Given your point here: Each amenity has a name, description, and image (an icon)*., I’m assuming you will need to go with #1.

    foreach ( $terms as $term ) {
    	$name = $term->name;
    	$desc = $term->description;
    	$icon = get_term_meta( $term->term_id, '_cmb_icon', true );
    	
    	echo $name;
    	echo $desc;
    	echo $icon;		
    }
    
    Thread Starter Phil

    (@outbavk)

    Friend, you saved the day. Thanks a lot! I just going to jump right in and give CMB2 framework five stars. Not only the script is awesome; but, the developers behind it too. Thumbs up!

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Welcome, and thanks for the review 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Retrieve custom taxonomy meta data in custom post type’ is closed to new replies.