Skip Navigation

[Resolved] In a view, only show terms from a specific hierarchical level

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a hierarchical taxonomy with multiple levels. I would like to create a View of this taxonomy and display terms only from one specific level of hierarchy, regardless of parent term.

Solution: Use a custom shortcode that produces a comma-separated list of city term IDs like 1,2,3,4. Then use this shortcode in a View of terms filtered by term ID as a shortcode attribute "terms". Here's a custom shortcode snippet:

// COMMA-SEPARATED LIST OF N-LEVEL TAXONOMY TERM IDS OR SLUGS
// n => Number, hierarchical level
// format => String "id" (default) or "slug"
// taxonomy => String "taxonomy-slug"
// Ex. [n-level-term n="2" format="slug" taxonomy="book-tax"]
function n_level_terms_func($atts) {
  $a = shortcode_atts( array(
      'n' => '0',
      'format'=>'id',
      'taxonomy' => ''
  ), $atts );
  $tax = $a['taxonomy'];
  $terms = get_terms( $tax, array( 'hide_empty' => false) );
  $returns = array();
  foreach( $terms as $term ) {
    if( isset($term->term_id) ){
      $ancestors = get_ancestors( $term->term_id, $tax );
      $count = count( $ancestors ) + 1;
      if( $count == $a['n'] )
      {
        $returns[] = $a['format'] == 'id' ? $term->term_id : $term->slug;
      }
    }
  }
 
  return implode( ',', $returns);
}
add_shortcode("n-level-terms", "n_level_terms_func");

Here's how you would add it to the View's shortcode attribute:

[wpv-view name="Filtered Term View" terms="[n-level-terms n='4' format='id' taxonomy='places']"]

Relevant Documentation:
https://developer.wordpress.org/reference/functions/get_terms/
https://developer.wordpress.org/reference/functions/get_ancestors/

This support ticket is created 5 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by romanB-3 5 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#1224111

Tell us what you are trying to do? : I have a taxonomy "places" with hierarchical relationship :
- 1rst level is "country" ;
- 2nd level is "state" ;
- 3rd level is "region" ;
- 4th level is "cities".
I need to set up a view that would only show cities.

I suppose I must setup a php function that would check if the current term has parent ; if it doesn't, then it's a country, so I don't consider it. If it does, then I check if it has grand-parent. Etc.

But I'm not quite sure how :
- to build this function ;
- to call it in my view ?

Thank you very much.

#1224155

Hi, there's nothing built-in to Views that will allow you to filter by an arbitrary hierarchical level. I think the best option is to use a custom shortcode that produces a comma-separated list of city term IDs like 1,2,3,4. Then use this shortcode in a View of terms filtered by term ID as a shortcode attribute "terms". Here's a custom shortcode snippet:

// COMMA-SEPARATED LIST OF N-LEVEL TAXONOMY TERM IDS OR SLUGS
// n => Number, hierarchical level
// format => String "id" (default) or "slug"
// taxonomy => String "taxonomy-slug"
// Ex. [n-level-term n="2" format="slug" taxonomy="book-tax"]
function n_level_terms_func($atts) {
  $a = shortcode_atts( array(
      'n' => '0',
      'format'=>'id',
      'taxonomy' => ''
  ), $atts );
  $tax = $a['taxonomy'];
  $terms = get_terms( $tax, array( 'hide_empty' => false) );
  $returns = array();
  foreach( $terms as $term ) {
    if( isset($term->term_id) ){
      $ancestors = get_ancestors( $term->term_id, $tax );
      $count = count( $ancestors ) + 1;
      if( $count == $a['n'] )
      {
        $returns[] = $a['format'] == 'id' ? $term->term_id : $term->slug;
      }
    }
  }

  return implode( ',', $returns);
}
add_shortcode("n-level-terms", "n_level_terms_func");

Here's how you would add it to the View's shortcode attribute:

[wpv-view name="Filtered Term View" terms="[n-level-terms n='4' format='id' taxonomy='places']"]
#1224160

Hello,

Thank you very much. That is great !

I'll open another thread in order to know how this solution can be extended.

Thank you.