Display a custom taxonomy as a dropdown on the edit posts page

When you create a custom taxonomy, seemingly the only options are to display it as a tag (non-hierarchical) or category (hierarchical). Is there a way to display them as a dropdown menu?

I realise that this an be done using add_meta_box() and actually manually adding it, but I’m looking for a builtin way (if one exists), to save lots of code! Thanks.

Related posts

Leave a Reply

5 comments

  1. Unfortunately there is no way of doing this with wp_register_taxonomy(). When you register a taxonomy, the default metabox is either checkboxes (hierarchical) or as a cloud (non-hierarchical).

    The only way round it is to de-register the existing metabox and add a new-one in its place. By carefully mimicking the ‘default’ metabox – you won’t have to process the data yourself, but let WordPress automatically handle adding / removing terms.

    The details of how to do this are given in this answer. You may also find this GitHub repository helpful (which now includes the ability of creating terms on the fly in the new metabox). The linked resources refer to creating a metabox with radio boxes, but it could be easily adapted for drop-down menus.

  2. You can add new taxonomy with your own metabox since wp 3.8 by meta_box_cb

    “drop_cat()” – consist of rebuild original core category metabox
    wp-admin/includes/meta-boxes.php

    function realty_type() {
    $args = array(
        'show_ui'                    => true,
        'meta_box_cb'                => 'drop_cat',
    );
    register_taxonomy( 'realty_type', array( 'YOUR_POST_TYPE' ), $args );
    
    }
    
    // Hook into the 'init' action
    add_action( 'init', 'realty_type', 0 );
    
    
    function drop_cat( $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);
    ?>
    <div id="taxonomy-<?php echo $taxonomy; ?>" class="acf-taxonomy-field categorydiv">
    
            <?php 
            $name = ( $taxonomy == 'category' ) ? 'post_category' : 'tax_input[' . $taxonomy . ']';
            echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
            ?>
            <? $term_obj = wp_get_object_terms($post->ID, $taxonomy ); //_log($term_obj[0]->term_id)?>
            <ul id="<?php echo $taxonomy; ?>checklist" data-wp-lists="list:<?php echo $taxonomy?>" class="categorychecklist form-no-clear">
                <?php //wp_terms_checklist($post->ID, array( 'taxonomy' => $taxonomy) ) ?>
            </ul>
    
            <?php wp_dropdown_categories( array( 'taxonomy' => $taxonomy, 'hide_empty' => 0, 'name' => "{$name}[]", 'selected' => $term_obj[0]->term_id, 'orderby' => 'name', 'hierarchical' => 0, 'show_option_none' => '&mdash;' ) ); ?>
    
    </div>
    <?php
    }
    
  3. Here you go:

    <select>
        <?php
           $tax_terms = get_terms('your_taxonomy_name', array('hide_empty' => '0'));      
           foreach ( $tax_terms as $tax_term ):  
              echo '<option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';   
           endforeach;
        ?>
    </select> 
    

    Delete $tax_terms second argument if you don’t want to display empty terms.

    Also use selected if you want want to save picked option 🙂

  4. I was having difficulty with the code provided by Alexufo, so I rewrote it based on the updated function in WP 4.1.2, Here’s my updated drop_cat function:

    //function below re-purposed from wp-admin/includes/meta-boxes.php - post_categories_meta_box()
    function drop_cat( $post, $box ) {
        $defaults = array( 'taxonomy' => 'category' );
        if ( ! isset( $box['args'] ) || ! is_array( $box['args'] ) ) {
            $args = array();
        } else {
            $args = $box['args'];
        }
        $r = wp_parse_args( $args, $defaults );
        $tax_name = esc_attr( $r['taxonomy'] );
        $taxonomy = get_taxonomy( $r['taxonomy'] );
        ?>
        <div id="taxonomy-<?php echo $tax_name; ?>" class="categorydiv">
    
        <?php //took out tabs for most recent here ?>
    
            <div id="<?php echo $tax_name; ?>-all">
                <?php
                $name = ( $tax_name == 'category' ) ? 'post_category' : 'tax_input[' . $tax_name . ']';
                echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
                ?>
                <ul id="<?php echo $tax_name; ?>checklist" data-wp-lists="list:<?php echo $tax_name; ?>" class="categorychecklist form-no-clear">
                    <?php //wp_terms_checklist( $post->ID, array( 'taxonomy' => $tax_name, 'popular_cats' => $popular_ids ) ); ?>
                </ul>
    
                <?php $term_obj = wp_get_object_terms($post->ID, $tax_name ); //_log($term_obj[0]->term_id) ?>
                <?php wp_dropdown_categories( array( 'taxonomy' => $tax_name, 'hide_empty' => 0, 'name' => "{$name}[]", 'selected' => $term_obj[0]->term_id, 'orderby' => 'name', 'hierarchical' => 0, 'show_option_none' => "Select $tax_name" ) ); ?>
    
            </div>
        <?php if ( current_user_can( $taxonomy->cap->edit_terms ) ) : 
                // removed code to add terms here dynamically, because doing so added a checkbox above the newly added drop menu, the drop menu would need to be re-rendered dynamically to display the newly added term ?>
            <?php endif; ?>
    
            <p><a href="<?php echo site_url(); ?>/wp-admin/edit-tags.php?taxonomy=<?php echo $tax_name ?>&post_type=YOUR_POST_TYPE">Add New</a></p>
        </div>
        <?php
    }
    

    The one issue I’m having (as indicated by my comments in the above function), is that I cannot use the WP core add term/category feature in the meta box, because it defaults to dynamically adding new terms as a checkbox above my select menu. In order to work with a select menu, the menu will need to be re-rendered after the new term is added. Something I’m sure I could figure out but will leave for another day.

  5. the above code have some issues on me, let me put the right code that worked for me

     'meta_box_cb' => 'drop_cat',
    

    your category definition , then put this code in here

    remember to put

    function drop_cat( $post, $box ) {
        if ( ! isset( $box['args'] ) || ! is_array( $box['args'] ) ) {
            $args = array();
        } else {
            $args = $box['args'];
        }
        $r = wp_parse_args( $args, $defaults );
        $tax_name = esc_attr( $r['taxonomy'] );
        $taxonomy = get_taxonomy( $r['taxonomy'] );
        ?>
        <div id="taxonomy-<?php echo $tax_name; ?>" class="categorydiv">
    
        <?php //took out tabs for most recent here ?>
    
            <div id="<?php echo $tax_name; ?>-all">
                <?php
                $name = ( $tax_name == 'category' ) ? 'post_category' : 'tax_input[' . $tax_name . ']';
                echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
                ?>
                <ul id="<?php echo $tax_name; ?>checklist" data-wp-lists="list:<?php echo $tax_name; ?>" class="categorychecklist form-no-clear">
                    <?php //wp_terms_checklist( $post->ID, array( 'taxonomy' => $tax_name, 'popular_cats' => $popular_ids ) ); ?>
                </ul>
    
                <?php $term_obj = wp_get_object_terms($post->ID, $tax_name ); //_log($term_obj[0]->term_id) 
              
                ?>
                <?php wp_dropdown_categories( array( 'taxonomy' => $tax_name, 'option_none_value' => 0 ,'id'=>$term_obj[0]->term_id, 'value_field'=> 'slug' , 'hide_empty' => 0, 'name' => "{$name}[]", 'selected' => $term_obj[0]->slug, 'orderby' => 'name', 'hierarchical' => 0, 'show_option_none' => "Select $tax_name" ) ); ?>
    
            </div>
        <?php if ( current_user_can( $taxonomy->cap->edit_terms ) ) : 
                // removed code to add terms here dynamically, because doing so added a checkbox above the newly added drop menu, the drop menu would need to be re-rendered dynamically to display the newly added term ?>
            <?php endif; ?>
    
            <p><a href="<?php echo site_url(); ?>/wp-admin/edit-tags.php?taxonomy=<?php echo $tax_name ?>&post_type=YOUR_POST_TYPE">Add New</a></p>
        </div>
        <?php
        }