5

I have 3 levels of categories, categories, sub categories and sub sub categories for custom post type. i am trying to show these categories in dropdown using cmb2, my code is only showing 2 levels of the categories and missing third level.

Category 1
 -- child category 1
 -- child category 2
    -- addon category 1
    -- addon category 2
 -- child category 3
 -- child category 4
    -- addon category 1
    -- addon category 2

Category 2
 -- child category 1
 -- child category 2
    -- addon category 1
    -- addon category 2
 -- child category 3
 -- child category 4
    -- addon category 1
    -- addon category 2

and I am using cmb2 to get these categories in select2 with multiselect option.

and write down the following code:

function gp_get_cmb_options_array_tax( $taxonomy, $args = array() ) {

    if ( empty( $taxonomy ) ) { return; }

    $defaults = array(
        'hide_empty' => 0,
    );

    $args = wp_parse_args( $args, $defaults );
    $terms = get_terms( $taxonomy, $args );

    /**
     * https://developer.wordpress.org/reference/functions/_get_term_hierarchy/
     */
    $hierarchy = _get_term_hierarchy( $taxonomy );

    $term_list = array();
    foreach ( $terms as $term ) {

        if( $term->parent ) {
            continue;
        }

        $term_list[ $term->term_id ] = $term->name;

        if( isset( $hierarchy[ $term->term_id ] ) ) {

            foreach ( $hierarchy[ $term->term_id ] as $child ) {

                $child = get_term( $child, $taxonomy );
                $term_list[ $child->term_id ] = $term->name . ' > ' . $child->name;

            }

        }

    }

    return $term_list;

}

showing only 2 levels and missing third level of categories

and dropdown is showing like:

Category 1
Category 1 > child category 1
Category 1 > child category 2
Category 1 > child category 3
Category 1 > child category 4

Category 2
Category 2 > child category 1
Category 2 > child category 2
Category 2 > child category 3
Category 2 > child category 4

Although it should show like

Category 1
Category 1 > child category 1
Category 1 > child category 2
Category 1 > child category 2 > addon category 1
Category 1 > child category 2 > addon category 2
Category 1 > child category 3
Category 1 > child category 4
Muzammil Hussain
  • 107
  • 1
  • 12

1 Answers1

2

You simply need to loop one level deeper:

function gp_get_cmb_options_array_tax( $taxonomy, $args = array() ) {

    if ( empty( $taxonomy ) ) { return; }

    $defaults = array(
        'hide_empty' => 0,
    );

    $args = wp_parse_args( $args, $defaults );
    $terms = get_terms( $taxonomy, $args );

    /**
     * https://developer.wordpress.org/reference/functions/_get_term_hierarchy/
     */
    $hierarchy = _get_term_hierarchy( $taxonomy );

    $term_list = array();
    foreach ( $terms as $term ) {

        if( $term->parent ) {
            continue;
        }

        $term_list[ $term->term_id ] = $term->name;

        if( isset( $hierarchy[ $term->term_id ] ) ) {

            foreach ( $hierarchy[ $term->term_id ] as $child ) {

                $child = get_term( $child, $taxonomy );
                $term_list[ $child->term_id ] = $term->name . ' > ' . $child->name;

                if( !isset( $hierarchy[ $child->term_id ] ) )
                    continue;

                foreach ($hierarchy[ $child->term_id ] as $subchild) {

                    $subchild = get_term( $subchild, $taxonomy );
                    $term_list[ $subchild->term_id ] = $term->name . ' > ' . $child->name. ' > ' .$subchild->name;

                }

            }

        }

    }

    return $term_list;

}
Oleg
  • 109
  • 2