Support » Plugin: CMB2 » A dropdown for taxonomy terms

  • Resolved vocmarcello


    i have read this document for get dropdown taxonomy term

    But this function get all taxonomy (parent children) all togheder


    Gets a number of terms and displays them as options

    @param CMB2_Field $field

    @return array An array of options that matches the CMB2 options array
    function cmb2_get_term_options( $field ) {
    $args = $field->args( ‘get_terms_args’ );
    $args = is_array( $args ) ? $args : array();

    $args = wp_parse_args( $args, array( ‘taxonomy’ => ‘category’ ) );

    $taxonomy = $args[‘taxonomy’];

    $terms = (array) cmb2_utils()->wp_at_least( ‘4.5.0’ )
    ? get_terms( $args )
    : get_terms( $taxonomy, $args );

    // Initate an empty array
    $term_options = array();
    if ( ! empty( $terms ) ) {
    foreach ( $terms as $term ) {
    $term_options[ $term->term_id ] = $term->name;

    return $term_options;

    View post on

    How can fix this?
    It’s good show the category 1 livel and after the category 2 livel

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Michael Beckwith


    The BenchPresser

    If I recall right, get_terms() doesn’t do anything for say nested arrays for the sake of “respecting” parent/child relationships. It’ll all just come back in the same array, and it’s also set to orderby ‘name’ by default. In your screenshot above, it’d make sense that it comes out like that because ‘C’ comes before ‘P’ in the alphabet.

    You will need to do some extra looping and queries with get_terms() to get things ordered the way you want. This is not going to end up being a CMB2 issue specifically, outside of the fact that it’s in a function that CMB2 provides. Ultimately, it’s a get_terms/WP question.


Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘A dropdown for taxonomy terms’ is closed to new replies.