Search code examples
taxonomywordpress

Wordpress: Dynamically query/filter posts by taxonomy term_id


I have a walker to display terms of a taxonomy (see post here). It's a hierarchical list that is 3 levels deep:

Taxonomy

  1. terms (number of posts incl. all sub levels)

    1.1. subterms (number of posts incl. subsubterms 1 & 2)

    1.1.1. subsubterms (number of posts)

    • display post with Category "Subsubterms"

    1.1.2 subsubterms (number of posts)

    • display post with Category "Subsubterms"

The terms display fine, but the posts on subsubterm-level do not display. The following code only displays post from the "Uncategorized" category.

add_filter( 'XYZ_index', 'return_XYZ_index' );

function return_XYZ_index()
{

  $taxonomies = array( 
    'XYZ'
  );

  $args = array(
    'orderby'           => 'name', 
    'order'             => 'ASC',
    'hide_empty'        => false,
    'fields'            => 'all',
    'parent'            => 0,
    'hierarchical'      => true,
    'child_of'          => 0,
    'pad_counts'        => false,
    'cache_domain'      => 'core'
  );

  $terms = get_terms($taxonomies, $args);    

  $return .= '<ul>';     

  foreach ( $terms as $term ) {

        $subterms = get_terms($taxonomies, array(
          'parent'   => $term->term_id,
          'hide_empty' => false
          ));

        //count posts in terms plus subterms
          $count = 0;
          $countargs = array(
            'child_of' => $term->term_ID,
            );
          $tax_terms = get_terms($taxonomies,$countargs);
          foreach ($tax_terms as $tax_term) {
            $count +=$tax_term->count;
            }

      // return terms 
      $return .= sprintf(
        '<li id="category-%1$s" class="toggle">%2$s  <span class="post-count">' . $count . '</span><span class="cat-description">%3$s</span>',       
        $term->term_id,
        $term->name,
        $term->description
      );

      //count posts in subterm
      $countsub = 0;
      $countsubargs = array(
        'child_of' => $subterm->term_id,
        );
      $tax_term_subs = get_terms($taxonomies, $countsubargs);
      foreach ($tax_term_subs as $tax_term_sub) {
        $countsub +=$tax_term_sub->count;
      }


        $return .= '<ul>';

        foreach ( $subterms as $subterm ) {

          $subsubterms = get_terms($taxonomies, array(
            'parent'   => $subterm->term_id,
            'hide_empty' => false
            ));            

          // return subterms
          $return .= sprintf(
          '<li id="category-%1$s" class="toggle">%2$s  <span class="post-count">' . $countsub . '</span><span class="cat-description">%3$s</span>',       
          $subterm->term_id,
          $subterm->name,
          $subterm->description
          );

          $return .= '<ul>';

          foreach ( $subsubterms as $subsubterm ) {

            // return subsubterms
            $return .= sprintf(
            '<li id="category-%1$s" class="toggle">%2$s <span class="post-count">%3$s</span><span class="cat-description">%4$s</span>',       
            $subsubterm->term_id,
            $subsubterm->name,
            $subsubterm->count,
            $subsubterm->description
            );

            // return posts (**EDITED CODE**)

            $postargs = array(
            'tax_query' => array(
              array(
                'taxonomy' =>'XYZ,
                'field' => 'id',
                'terms' =>  $subsubterm->term_id
                )
              )
            );
            $post_query = new WP_Query( $args ); 
             if ( $post_query->have_posts() ) : 
                $return .= '<ul>';
                 while ( $post_query->have_posts() ) : $post_query->the_post();
                    $return .= '<li><a class="link" href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
                endwhile;
                $return .= '</ul>';

             wp_reset_postdata();
             else:
             endif;

            $return .= '</li>'; //end subsubterms li
          }

          $return .= '</ul>'; //end subsubterms ul

        $return .= '</li>'; //end subterms li 
        }            

        $return .= '</ul>'; //end subterms ul

      $return .= '</li>'; //end terms li
    } //end foreach term        

    $return .= '</ul>';

  return $return;
}

I'm not sure if this part is even possible:

'term' => $subsubterm->term_id

Thanks!


Solution

  • Replace category with your actual taxonomy. Use following code:

              $args = array(
                  'tax_query' => array(
                  array(
                  'taxonomy' =>'category',
                  'field' => 'id',
                  'terms' =>  $subsubterm->term_id
                  )
                )
          );
    $post_query = new WP_Query( $args ); 
     if ( $post_query->have_posts() ) : 
        $return .= '<ul>';
         while ( $post_query->have_posts() ) : $post_query->the_post();
            $return .= '<li><a class="link" href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
        endwhile;
        $return .= '</ul>';
    
     wp_reset_postdata();
    else:
     endif;
    

    Or you can use complete code provided below:

    function return_XYZ_index()
    {
    
      $taxonomies = array( 
        'XYZ'
      );
    
      $args = array(
        'orderby'           => 'name', 
        'order'             => 'ASC',
        'hide_empty'        => false,
        'fields'            => 'all',
        'parent'            => 0,
        'hierarchical'      => true,
        'child_of'          => 0,
        'pad_counts'        => false,
        'cache_domain'      => 'core'
      );
    
      $terms = get_terms($taxonomies, $args);
    
      $return .= '<ul>'; 
    
      foreach ( $terms as $term ) {
    
            $subterms = get_terms($taxonomies, array(
              'parent'   => $term->term_id,
              'hide_empty' => false
              ));
    
            //count posts in terms plus subterms
              $count = 0;
              $countargs = array(
                'child_of' => $term->term_ID,
                );
              $tax_terms = get_terms($taxonomies,$countargs);
              foreach ($tax_terms as $tax_term) {
                $count +=$tax_term->count;
                }
    
          // return terms 
          $return .= sprintf(
            '<li id="category-%1$s" class="toggle">%2$s  <span class="post-count">' . $count . '</span><span class="cat-description">%3$s</span>',       
            $term->term_id,
            $term->name,
            $term->description
          );
    
          //count posts in subterm
          $countsub = 0;
          $countsubargs = array(
            'child_of' => $subterm->term_id,
            );
          $tax_term_subs = get_terms($taxonomies, $countsubargs);
          foreach ($tax_term_subs as $tax_term_sub) {
            $countsub +=$tax_term_sub->count;
          }
    
    
            $return .= '<ul>';
    
            foreach ( $subterms as $subterm ) {
    
              $subsubterms = get_terms($taxonomies, array(
                'parent'   => $subterm->term_id,
                'hide_empty' => false
                ));            
    
              // return subterms
              $return .= sprintf(
              '<li id="category-%1$s" class="toggle">%2$s  <span class="post-count">' . $countsub . '</span><span class="cat-description">%3$s</span>',       
              $subterm->term_id,
              $subterm->name,
              $subterm->description
              );
    
              $return .= '<ul>';
    
              foreach ( $subsubterms as $subsubterm ) {
    
                // return subsubterms
                $return .= sprintf(
                '<li id="category-%1$s" class="toggle">%2$s <span class="post-count">%3$s</span><span class="cat-description">%4$s</span>',       
                $subsubterm->term_id,
                $subsubterm->name,
                $subsubterm->count,
                $subsubterm->description
                );
    
                // return posts
                 $args = array(
                      'tax_query' => array(
                      array(
                      'taxonomy' =>'category',
                      'field' => 'id',
                      'terms' =>  $subsubterm->term_id
                      )
                    )
              );
        $post_query = new WP_Query( $args );  if ( $post_query->have_posts() ) : 
                  $return .= '<ul>';
                  while ( $post_query->have_posts() ) : $post_query->the_post(); 
                    $return .= '<li><a class="link" href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
                  endwhile;
                  $return .= '</ul>';
    
                  wp_reset_postdata();
    
                else:
                endif;
    
                $return .= '</li>'; //end subsubterms li
              }
    
              $return .= '</ul>'; //end subsubterms ul
    
            $return .= '</li>'; //end subterms li 
            }            
    
            $return .= '</ul>'; //end subterms ul
    
          $return .= '</li>'; //end terms li
        } //end foreach term        
    
        $return .= '</ul>';
    
      return $return;
    }