Home > Blockchain >  Custom Taxonomy for CPT not showing tags in admin
Custom Taxonomy for CPT not showing tags in admin

Time:05-31

I have created a Custom post type called 'event' and I'm adding a custom taxonomy for this post type. I have created 3 tags in the custom taxonomy but when I am editing a new event post, the taxonomies are not returned and it shows "no tags found".

below is the code for the cpt and taxonomy. Am I missing something.

-- create the custom post type

   function bps_event_posttype() {
$labels = array(
    'name'               => _x( 'Events', 'post type general name' ),
    'singular_name'      => _x( 'Event', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'Event' ),
    'add_new_item'       => __( 'Event' ),
    'edit_item'          => __( 'Edit Event' ),
    'new_item'           => __( 'New Event' ),
    'all_items'          => __( 'All Events' ),
    'view_item'          => __( 'View Event' ),
    'search_items'       => __( 'Search Events' ),
    'not_found'          => __( 'No Events found' ),
    'not_found_in_trash' => __( 'No Events found in the Trash' ), 
    'parent_item_colon'  => '',
     'menu_name'          => 'Events');
$args = array(
    'labels'        => $labels,
    'description'   => 'custom event',
    'public'        => true,
    'exclude_from_search' => false,
    'query_var' => true,
    'show_in_menu' => true,
    'menu_position' => 4,
    'capability_type' => 'post',
    'rewrite' => array( 'slug' => 'events', 'with_front' => false ),
     'publicly_queryable' => true,
    'supports'      => array( 'title', 'editor', 'thumbnail', 
    'excerpt','page-attributes'),
    'taxonomies' => array('eventtype','location'),
    'hierarchical' => false,
    'has_archive' => true

    );
    register_post_type( 'event', $args );   
    }
    add_action( 'init', 'bps_event_posttype' );

-- create the taxonomy and assign it to the event cpt

    function add_event_type_taxonomy() {

    register_taxonomy('eventtype', array('event'), array(


'public' => false,
'show_admin_column' => true,
 'show_ui' => true, 
  'query_var' => true,

'labels' => array(
  'name' => _x( 'Event Types', 'taxonomy general name' ),
  'singular_name' => _x( 'Event Type', 'taxonomy singular name' ),
  'search_items' =>  __( 'Search Event Types' ),
  'all_items' => __( 'All Event Types' ),
  'parent_item' => __( 'Parent Event Type' ),
  'parent_item_colon' => __( 'Parent Event Type:' ),
  'edit_item' => __( 'Edit Event Type' ),
  'update_item' => __( 'Update Event Type' ),
  'add_new_item' => __( 'Add New Event Type' ),
  'new_item_name' => __( 'New Event Type Name' ),
  'menu_name' => __( 'Event Types' ),
),

'rewrite' => array(
  'slug' => 'types', 
  'with_front' => false, 
  'hierarchical' => false 
   ),
    ));
    }
    add_action( 'init', 'add_event_type_taxonomy', 0 );

    register_taxonomy_for_object_type('eventtype', 'event');

When editing a event post, if I select "Choose from the most used tags", it displays "No tags found." but there are 3 tags in the taxonomy edit screen.

CodePudding user response:

I have added your code in my localhost and checked that everything is okay in your coding part, there is nothing wrong, but what you have missed it that you need to select the tags at least one single event to see it in the "choose from the most used tags" area. For the very first time it will not show any tags there. Please see the screenshot https://prnt.sc/UujJXicZ729G . It is perfectly showing the tags. Hope you will be able to select tags from the chosen area after selecting a tags once.

  • Related