title: Wordpress: Dynamically query/filter posts by taxonomy term_id
body: <p>I have a walker to display terms of a taxonomy (see post <a href="https://stackoverflow.com/questions/26732017/wordpress-hierarchical-list-of-taxonomy-terms">here</a>). It's a hierarchical list that is 3 levels deep:</p>
  
  <p>Taxonomy</p>
  
  <ol>
  <li><p>terms (number of posts incl. all sub levels)</p>
  
  <p>1.1. subterms (number of posts incl. subsubterms 1 &amp; 2)</p>
  
  <p>1.1.1. subsubterms (number of posts)</p>
  
  <ul>
  <li>display post with Category "Subsubterms"</li>
  </ul>
  
  <p>1.1.2 subsubterms (number of posts)</p>
  
  <ul>
  <li>display post with Category "Subsubterms"</li>
  </ul></li>
  </ol>
  
  <p>The terms display fine, but the posts on subsubterm-level do not display. 
  The following code only displays post from the "Uncategorized" category. </p>
  
  <pre><code>add_filter( 'XYZ_index', 'return_XYZ_index' );
  
  function return_XYZ_index()
  {
  
    $taxonomies = array( 
      'XYZ'
    );
  
    $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 ) {
  
          $subterms = get_terms($taxonomies, array(
            'parent'   =&gt; $term-&gt;term_id,
            'hide_empty' =&gt; false
            ));
  
          //count posts in terms plus subterms
            $count = 0;
            $countargs = array(
              'child_of' =&gt; $term-&gt;term_ID,
              );
            $tax_terms = get_terms($taxonomies,$countargs);
            foreach ($tax_terms as $tax_term) {
              $count +=$tax_term-&gt;count;
              }
  
        // return terms 
        $return .= sprintf(
          '&lt;li id="category-%1$s" class="toggle"&gt;%2$s  &lt;span class="post-count"&gt;' . $count . '&lt;/span&gt;&lt;span class="cat-description"&gt;%3$s&lt;/span&gt;',       
          $term-&gt;term_id,
          $term-&gt;name,
          $term-&gt;description
        );
  
        //count posts in subterm
        $countsub = 0;
        $countsubargs = array(
          'child_of' =&gt; $subterm-&gt;term_id,
          );
        $tax_term_subs = get_terms($taxonomies, $countsubargs);
        foreach ($tax_term_subs as $tax_term_sub) {
          $countsub +=$tax_term_sub-&gt;count;
        }
  
  
          $return .= '&lt;ul&gt;';
  
          foreach ( $subterms as $subterm ) {
  
            $subsubterms = get_terms($taxonomies, array(
              'parent'   =&gt; $subterm-&gt;term_id,
              'hide_empty' =&gt; false
              ));            
  
            // return subterms
            $return .= sprintf(
            '&lt;li id="category-%1$s" class="toggle"&gt;%2$s  &lt;span class="post-count"&gt;' . $countsub . '&lt;/span&gt;&lt;span class="cat-description"&gt;%3$s&lt;/span&gt;',       
            $subterm-&gt;term_id,
            $subterm-&gt;name,
            $subterm-&gt;description
            );
  
            $return .= '&lt;ul&gt;';
  
            foreach ( $subsubterms as $subsubterm ) {
  
              // return subsubterms
              $return .= sprintf(
              '&lt;li id="category-%1$s" class="toggle"&gt;%2$s &lt;span class="post-count"&gt;%3$s&lt;/span&gt;&lt;span class="cat-description"&gt;%4$s&lt;/span&gt;',       
              $subsubterm-&gt;term_id,
              $subsubterm-&gt;name,
              $subsubterm-&gt;count,
              $subsubterm-&gt;description
              );
  
              // return posts (**EDITED CODE**)
  
              $postargs = array(
              'tax_query' =&gt; array(
                array(
                  'taxonomy' =&gt;'XYZ,
                  'field' =&gt; 'id',
                  'terms' =&gt;  $subsubterm-&gt;term_id
                  )
                )
              );
              $post_query = new WP_Query( $args ); 
               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;
  
              $return .= '&lt;/li&gt;'; //end subsubterms li
            }
  
            $return .= '&lt;/ul&gt;'; //end subsubterms ul
  
          $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>I'm not sure if this part is even possible:</p>
  
  <pre><code>'term' =&gt; $subsubterm-&gt;term_id
  </code></pre>
  
  <p>Thanks!</p>