wp_dropdown_categories in custom post type with custom taxonomy

get post id

    $post_id=get_the_ID();

get selected region

    $terms = wp_get_post_terms( $post_id, $taxonomy );

    $selected_id='';

    if(isset($terms[0]->term_id)){

        $selected_id=$terms[0]->term_id;
    }

build hierarchical dropdown

wp_dropdown_categories( array(
    'show_option_all'    => 'Choose a region',
    'show_option_none'   => '',
    'orderby'            => 'ID', 
    'order'              => 'ASC',
    'show_count'         => 0,
    'hide_empty'         => 0, 
    'child_of'           => 0,
    'exclude'            => '',
    'echo'               => 1,
    'selected'           => $selected_id,
    'hierarchical'       => 1, 
    'name'               => 'tax_input['.$taxonomy.'][]',     // important
    'id'                 => $id,
    'class'              => 'form-no-clear',
    'depth'              => 0,
    'tab_index'          => 0,
    'taxonomy'           => $taxonomy,
    'hide_if_empty'      => true
) );

Leave a Comment