Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Show Drupal Content Only With Terms of Specific Depth

Parent Feed: 
Show Drupal Content Only With Terms of Specific Depth

It is possible to allow Views to show only content that has child terms from a taxonomy?

Yes, it is. This is similar but slightly different to our tutorial on using Views to show only child terms.

First, you need to have your site set up correctly. You will need:

  • A taxonomy that has parent and child terms, as in this example below.
  • A content type that uses this taxonomy as a "Term reference" field. 
  • Content that is tagged with these taxonomy terms.
A Drupal taxonomy with child terms

Let's show you how to set up Views so that only content with child terms. In this example, the content needs terms such as Amphibians, Birds, Fish, Mammals, Reptiles.

  • Go to Structure > Views > Add new view.
  • Create a View that shows Content of the type that you want to show:
Creating a new view to show content with child terms
  • Click "Add" next to Relationships.
Adding a relationship to Views in Drupal
  • Search for "Content: Taxonomy terms on node":
Adding Content: Taxonomy terms on node to Views
  • Choose your taxonomy.
  • Click "Apply (All displays)" to save your relationship.
Choosing a taxonomy in a Views relationship
  • Click "Add" next to Filter Criteria.
Adding a filter criteria to Views
  • Search for "Taxonomy term: Parent term".
  • Click "Apply (All displays)".
Adding Taxonomy term: Parent term as a Views filter

Now the "Value" in this next step the key to the process.

  • The top level taxonomy terms have a value 0.
  • The second level terms have a value of 1.
  • The third level terms have a value of 2 etc.

This tutorial on Delta in Drupal explains more.

So if we want to ignore the parent terms, we choose "Is greater than or equal to" and then enter a "Value" of 1.

Configuring a Taxonomy term: Parent term filter
  • Save the filter and save your view.
  • You should now see that the parent terms are ignored:
A Drupal view showing only content with a child term

About the author

Steve is the founder of OSTraining. Originally from the UK, he now lives in Sarasota in the USA. He was a teacher for many years before starting OSTraining. Steve wrote the best-selling Drupal and Joomla books.


Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web