title: Wordpress: hierarchical list of taxonomy terms
body: <p>I am hitting a wall here, although it sounds pretty simple: I want to return a hierarchical list of custom post type taxonomy terms. What I get is the first level of terms and nested uls. But the sub terms are not showing. Any ideas?</p>
  
  <p>Here's the code:</p>
  
  <pre><code>function return_terms_index() {
    $taxonomies = array( 
      'taxonomy_name',
    );
  
    $args = array(
      'orderby'           =&gt; 'name', 
      'order'             =&gt; 'ASC',
      'hide_empty'        =&gt; false, 
      'fields'            =&gt; 'all',
      'parent'            =&gt; 0,
      'hierarchical'      =&gt; true,
      'child_of'          =&gt; 0,
      'pad_counts'        =&gt; false,
      'cache_domain'      =&gt; 'core'    
    );
  
    $terms = get_terms($taxonomies, $args);
  
    $return .= '&lt;ul&gt;'; 
  
      foreach ( $terms as $term ) {
  
        // return terms (working)
        $return .= sprintf(
          '&lt;li id="category-%1$s" class="toggle"&gt;%2$s &lt;span class="cat-description"&gt;%3$s&lt;/span&gt;',       
          $term-&gt;term_id,
          $term-&gt;name,
          $term-&gt;description
        );
  
          $subterms = get_terms( array(
            'parent'   =&gt; $term-&gt;term_id,
            'hide_empty' =&gt; false
            ));
  
          $return .= '&lt;ul&gt;';
  
          foreach ( $subterms as $subterm ) {
  
            //return sub terms (not working :( )
            $return .= sprintf(
            '&lt;li id="category-%1$s" class="toggle"&gt;%2$s &lt;span class="cat-description"&gt;%3$s&lt;/span&gt;',       
            $subterm-&gt;term_id,
            $subterm-&gt;name,
            $subterm-&gt;description
            );
  
            $return .= '&lt;/li&gt;'; //end subterms li
          }            
  
          $return .= '&lt;/ul&gt;'; //end subterms ul
  
        $return .= '&lt;/li&gt;'; //end terms li
      } //end foreach term
  
    $return .= '&lt;/ul&gt;';
  
  return $return;
  }
  </code></pre>
  
  <p>Thanks!</p>
  
  <p><strong>Edit:</strong> here's the output.</p>
  
  <pre><code>&lt;ul&gt;
    &lt;li id="category-176"&gt;
      1. &lt;span class="post-count"&gt;0&lt;/span&gt;&lt;span class="cat-description" style="display: none;"&gt;Description&lt;/span&gt;
      &lt;ul id="subTerm-176" style="display: block;"&gt;&lt;/ul&gt;
    &lt;/li&gt;
    &lt;li id="category-49"&gt;
      2. &lt;span class="post-count"&gt;0&lt;/span&gt;&lt;span class="cat-description" style="display: none;"&gt;Langtitel/Beschreibung&lt;/span&gt;
      &lt;ul id="subTerm-49" style="display: none;"&gt;&lt;/ul&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p><strong>Edit:</strong> taxonomies are returned in hierarchical list now, YAY! 
  But I want to query and display posts of third level taxonomy terms as well and this bit of code doesn't do the trick.</p>
  
  <pre><code>$post_query = new WP_Query($taxonomies, array( 
    'term' =&gt; $subsubterm-&gt;term_id 
    )); ?&gt;
  
    &lt;?php if ( $post_query-&gt;have_posts() ) : 
    $return .= '&lt;ul&gt;';
      while ( $post_query-&gt;have_posts() ) : $post_query-&gt;the_post(); 
      $return .= '&lt;li&gt;&lt;a class="link" href="' . get_permalink() . '"&gt;' . get_the_title() . '&lt;/a&gt;&lt;/li&gt;' . "\n";
      endwhile;
    $return .= '&lt;/ul&gt;';
  
  wp_reset_postdata();
  else:
  endif;
  </code></pre>
  
  <p>It has to be dynamic, so I can't specify a term by name/slug. But is this even possible?</p>
  
  <pre><code>'term' =&gt; $subsubterm-&gt;term_id
  </code></pre>
  
  <p>Thanks again.</p>