Loop through nested array of child elements wordpress get_terms and display hierarchy

I looked many guides over the net but nothing really completes the search what i need.

I am making custom taxonomy list for custom post type, and that taxonomy is hierarchical and should be basically displayed as category, but i want to point out subcategories or child items in select dropdown, something similarly like wordpress wp_list_categories, except i need custom output with only id and name because of dropdown list.

Read More

This is so far where i go, i managed to get them in line but as there are many subcategories i need to make it somehow dynamic.

enter image description here

I have found custom function which get whole list of categories and shift array to arrange them by parent-child relationship

function get_taxonomy_hierarchy( $taxonomy, $parent = 0 ) {
 // only 1 taxonomy
  $taxonomy = is_array( $taxonomy ) ? array_shift( $taxonomy ) : $taxonomy;

  // get all direct decendents of the $parent
  $terms = get_terms( $taxonomy, array( 'parent' => $parent, 'hide_empty' => false ) );

  // prepare a new array.  these are the children of $parent
  // we'll ultimately copy all the $terms into this new array, but only after they
  // find their own children
  $children = array();

 // go through all the direct decendents of $parent, and gather their children
  foreach ( $terms as $term ){
    // recurse to get the direct decendents of "this" term
   $term->children = get_taxonomy_hierarchy( $taxonomy, $term->term_id );

   // add the term to our new array
   $children[ $term->term_id ] = $term;
  }

  // send the results back to the caller
  return $children;
}

And the output of that is next:

(sorry for posting on pastebin but array is really too long)
http://pastebin.com/ekZAFb3C

So from that i can see the child categories and output foreach parrent-child but i don’t know how to out put it multi level and recursively, i just did it manually which is not quite good idea.

                        <select class="select2 form-control" data-placeholder="All Categories" data-allow-clear="true">
                        <option></option> 
                        <?php $hierarchy = get_taxonomy_hierarchy( 'listings_category' );
                        foreach ($hierarchy as $list) {
                            echo '<option value="' . $list->term_id . '"><strong>' . $list->name . '</strong></option>';
                            if (!empty($list->children)) {
                               foreach ($list->children as $children) {
                                    echo '<option value="' . $children->term_id . '">- ' . $children->name . '</option>';
                                            if (!empty($children->children)) {
                                           foreach ($children->children as $subchildren) {
                                                echo '<option value="' . $subchildren->term_id . '">-- ' . $subchildren->name . '</option>';
                                           }
                                        }
                               }
                            }
                        }

                        ?>
                    </select>

So any ideas how can i make it dynamically and to have parent-child relationship

Related posts

1 comment

  1. You can use wordpress inbuilt method wp_dropdown_categories()

    Here is the example..

    $args = [
                'show_option_all'    => 'All',
                'orderby'            => 'ID', 
                'order'              => 'ASC',
                'hide_empty'         => 0, 
                'selected'           => isset( $_REQUEST['your drop down name'] ) ? $_REQUEST['your drop down name'] : '' ,
                'hierarchical'       => 1, 
                'name'               => 'your drop down name',
                'taxonomy'           => 'your custom post type name',
                'hide_if_empty'      => false,
                'value_field'        => 'term_id',
            ];
            wp_dropdown_categories( $args );
    

Comments are closed.