How to Display WordPress Custom Taxonomy in Dropdown

In this article, we will discuss “How to Display WordPress Custom Taxonomy in Dropdown”. As we know, WordPress is the most usable CMS (Content Management System) in the world. It provides a lot of features to the developer to make the development easy. And we can also apply changes to the default admin layout. Today, I will explain to you to display custom taxonomy in the dropdown view instead of the checkbox list.

Register Custom Taxonomy

Register custom taxonomy “topics”, add the following code into the “functions.php”.

// Register Custom Taxonomy Topics
function custom_taxonomy_topics() {

  $labels = array(
    'name'                       => _x( 'Topics', 'text_domain' ),
    'singular_name'              => _x( 'Topic', 'text_domain' ),
    'menu_name'                  => __( 'Topics', 'text_domain' ),
    'all_items'                  => __( 'All Topics', 'text_domain' ),
    'parent_item'                => __( 'Parent Topic', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Topic:', 'text_domain' ),
    'new_item_name'              => __( 'New Topic Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Topic', 'text_domain' ),
    'edit_item'                  => __( 'Edit Topic', 'text_domain' ),
    'update_item'                => __( 'Update Topic', 'text_domain' ),
    'view_item'                  => __( 'View Topic', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate topics with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove topics', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    'popular_items'              => __( 'Popular Topics', 'text_domain' ),
    'search_items'               => __( 'Search Topics', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
    'no_terms'                   => __( 'No topics', 'text_domain' ),
    'items_list'                 => __( 'Topics list', 'text_domain' ),
    'items_list_navigation'      => __( 'Topics list navigation', 'text_domain' ),
  );
  $args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'meta_box_cb'  		 => 'cb_taxonomy_select_meta_box',
  );
  register_taxonomy( 'topics', array('post'), $args );

}
add_action( 'init', 'custom_taxonomy_topics', 0 );

You can get more information on the “register_taxonomy” here. Here, I’m using “meta_box_cb”  callback function. We have to pass the function name as value, otherwise, it’s set as null by default. This is an optional argument of the register_taxonomy function.

Dropdown Meta Box Function

Add the following code into the “functions.php”.

/**
 * Display taxonomy selection as dropdown
 *
 * @param WP_Post $post
 * @param array $box
 */
function cb_taxonomy_select_meta_box($post, $box) 
{
  $defaults = array('taxonomy' => 'category');
  
  if (!isset($box['args']) || !is_array($box['args']))
      $args = array();
  else
      $args = $box['args'];
  
  extract(wp_parse_args($args, $defaults), EXTR_SKIP);
  
  $tax = get_taxonomy($taxonomy);
  $selected = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids'));
  $hierarchical = $tax->hierarchical;
  ?>
  <div id="taxonomy-<?php echo $taxonomy; ?>" class="selectdiv">
    <?php 
      if (current_user_can($tax->cap->edit_terms)): 
        if ($hierarchical) {
          wp_dropdown_categories(array(
           	'taxonomy' => $taxonomy,
           	'class' => 'widefat',
         	'hide_empty' => 0, 
           	'name' => "tax_input[$taxonomy][]",
           	'selected' => count($selected) >= 1 ? $selected[0] : '',
           	'orderby' => 'name', 
           	'hierarchical' => 1, 
           	'show_option_all' => " "
          ));
        } else {?>
          <select name="<?php echo "tax_input[$taxonomy][]"; ?>" class="widefat">
            <option value="0"></option>
            <?php foreach (get_terms($taxonomy, array('hide_empty' => false)) as $term): ?>
              <option value="<?php echo esc_attr($term->slug); ?>" <?php echo selected($term->term_id, count($selected) >= 1 ? $selected[0] : ''); ?>><?php echo esc_html($term->name); ?></option>
            <?php endforeach; ?>
          </select>
        <?php 
        }
      endif; 
    ?>
  </div>
  <?php
}

After updating the code, it looks like:

Final Words

I hope you have enjoyed this tutorial, Today we’ve explored How to Display WordPress Custom Taxonomy in Dropdown. We will discuss more on the WordPress and its features in our coming tutorials. If you have any query please feel free to add in the comment area or send me your feedback.

Stay Home, Stay Safe, Keep Learning 🙂

You may like:

How to Create Taxonomy for Users in WordPress

Laravel Image Processing – Intervention Image Package

Laravel Logging Guzzle Requests in File

If you like our content, please consider buying us a coffee.
Thank you for your support!
Buy Me a Coffee

WordPressWordpress Tips & Tricks
Comments (2)
Add Comment
  • neha

    how to add new categories option show in this dropdown

  • neha

    how to add new categories option show in this dropdown fields