get taxonomy terms for parent and child

I have a function set up that prints the taxonomy term name and slug for every products-category taxonomy term present. This works great, but it’s just displaying them alphabetically like so (regardless of if they are a parent / child taxonomy term):

Parent Category 2
Parent Category 1
Child Category 3
Parent Category 3
Child Category 2
Child Category 1 

Whereas I’m after a structure more like this:

—Parent Category 1
Child Category 1
Child Category 2
Child Category 3

—Parent Category 2
Child Category 1
Child Category 2
Child Category 3

—Parent Category 3
Child Category 1
Child Category 2
Child Category 3

So the children terms of each taxonomy term sit underneath, so you know what parent they belong to. My markup is as follows:

$args = array(
    'hide_empty' => false
 $terms = get_terms("products-category");
 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
     foreach ( $terms as $term ) { ?>
      <option value=".<?php echo $term->slug; ?>" data-hook="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
    <?php }
 } ?>   

Any suggestions on how to achieve this would be greatly appreciated!

Solutions Collecting From Web of "get taxonomy terms for parent and child"

You should have two foreach loops. One for getting parent taxonomy terms, and second for getting child taxonomy terms.

In the second foreach you need to specify the parent taxonomy term ID which is $parent_term->term_id from the first foreach loop.

foreach( get_terms( 'products-category', array( 'hide_empty' => false, 'parent' => 0 ) ) as $parent_term ) {
  // display top level term name
  echo $parent_term->name . '<br>';

  foreach( get_terms( 'products-category', array( 'hide_empty' => false, 'parent' => $parent_term->term_id ) ) as $child_term ) {
    // display name of all childs of the parent term
    echo $child_term->name . '<br>';


You can simply make use wp_list_categories to display your list. It is extremely versatile and can easily be modified to suite your specific needs. Although the name suggest that it is just for the purpose of the build in taxonomy category, it is not. It can be used for any taxonomy

You’ll have to work through examples etc and modify as needed. Here is a simple example that you can use to display your list