Answers.Body: <p>I can't think of a way to do this with a single query. Category queries have a <a href="https://craftcms.com/docs/3.x/categories.html#leaves" rel="nofollow noreferrer">leaves</a> method to narrow the query to leaf nodes (categories with no descendants), but that won't work in your case since you it won't give you categories that do have descendents that aren't selected on the entry. It would probably be possible to construct a query to achieve this manually, but that'll be a pretty low-level operation, so I would avoid it if it's not absolutely necessary for performance reasons.</p>
  <p>So you'll have to manually filter the list of categories. One way I can think of to do this:</p>
  <ol>
  <li>Get all selected categories of the entry.</li>
  <li>Filter out all categories where one of the other categories is a descendent of itself.</li>
  </ol>
  <p>Something like this should work (untested, might need some adjustments):</p>
  <pre><code>{% set categories = item.categoriaDir.all() %}
  {% set categoryIds = categories|column('id') %}
  {% set filteredCategories = categories|filter(
      cat =&gt; cat.getDescendants().id(categoryIds).exists()
  ) %}
  </code></pre>
  <p>Note that this suffers from the <a href="https://craftcms.com/docs/3.x/dev/eager-loading-elements.html" rel="nofollow noreferrer">N+1 problem</a>, so you'll want to cache this part of the template.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-06-02T15:02:28.740Z
Answers.Text: Thanks for your response Moritz, i will check it out. Yesterday night some idea came to me: since only last category in the hierarchy will be used from the admin users to relate, they wont use for example a category level 1 that have childs options, maybe i can filter the category list to categories that DON´T HAVE DESCENDANTS, because if it has it won´t be selected, make sense?
  So case 1: selected category level 1: don´t have descendants.
  Case 2: selected category level 2: category level 1 have descendant, i must avoid it
  
  I´m checking LEAVES
Answers.UserDisplayName: Sebastián.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-06-02T15:12:46.500Z
Answers.Text: You answer point me in the right direction, leaves do the job in my case, thank you!
Answers.UserDisplayName: Sebastián.
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-06-02T08:19:25.983Z
Answers.LastActivityDate: 2021-06-02T08:19:25.983Z
Answers.UserDisplayName: MoritzLost
Body: <p>I saw some similar questions, but not specific like this.</p>
  <p>I have a list of categories, some have only 1 level, other go to 2nd level hierarchy.</p>
  <p>Entries can <strong>have up to 4 categories related</strong>, and they can be categories of level 1 ( that don´t have childs ) or level 2.</p>
  <p>In a list of entries ( loop ), i need to <strong>list all the related categories for each entry, but only the last level of each one</strong>, for example if is related to Real Estate -&gt; Architecture, i don´t want to list both, just Architecture.</p>
  <p>Any help is appreciated:</p>
  <pre><code>{% for item in listado %}
      {% set imagenDir = item.imagenDir[0] ?? null %}
      {% set catDir = item.categoriaDir.all() %}
      {% set zonaDir = item.zonaDir.all() %}
      {% set logoDir = item.logoDir[0] ?? null %}
      ...
      {% if catDir %}
      
      {% for item in catDir %}
      &lt;span class=&quot;uk-label label-cat&quot;&gt;{{ item.title }}&lt;/span&gt;
      {% endfor %}
      
      {% endif %}
  </code></pre>
  <p><a href="https://i.sstatic.net/OklLs.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/OklLs.png" alt="enter image description here" /></a></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-06-02T08:07:14.007Z
Comments.Text: Please use english in your code example or add comments to explain what the variables are for. I don't speak spanish and have no idea what all the variables mean.
Comments.UserDisplayName: MoritzLost
Tags: <craft3>
Title: Get a list of last level categories in an entry - Craft CMS 3
ThreadText: # Get a list of last level categories in an entry - Craft CMS 3
  
  (Asked by: username_0 on 2021-06-02)
  
  I saw some similar questions, but not specific like this.
  
  I have a list of categories, some have only 1 level, other go to 2nd level hierarchy.
  
  Entries can **have up to 4 categories related**, and they can be categories of level 1 ( that don´t have childs ) or level 2.
  
  In a list of entries ( loop ), i need to **list all the related categories for each entry, but only the last level of each one**, for example if is related to Real Estate -> Architecture, i don´t want to list both, just Architecture.
  
  Any help is appreciated:
  
  ```
  {% for item in listado %}
      {% set imagenDir = item.imagenDir[0] ?? null %}
      {% set catDir = item.categoriaDir.all() %}
      {% set zonaDir = item.zonaDir.all() %}
      {% set logoDir = item.logoDir[0] ?? null %}
      ...
      {% if catDir %}
      
      {% for item in catDir %}
      <span class="uk-label label-cat">{{ item.title }}</span>
      {% endfor %}
      
      {% endif %}
  
  ```
  
  [![enter image description here](https://i.sstatic.net/OklLs.png)](https://i.sstatic.net/OklLs.png)
  
  **Tags:** craft3
  
  > **Comments:**
  > **username_1**: Please use english in your code example or add comments to explain what the variables are for. I don't speak spanish and have no idea what all the variables mean.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-06-02. Score: 2 | Accepted answer
  
  I can't think of a way to do this with a single query. Category queries have a [leaves](https://craftcms.com/docs/3.x/categories.html#leaves) method to narrow the query to leaf nodes (categories with no descendants), but that won't work in your case since you it won't give you categories that do have descendents that aren't selected on the entry. It would probably be possible to construct a query to achieve this manually, but that'll be a pretty low-level operation, so I would avoid it if it's not absolutely necessary for performance reasons.
  
  So you'll have to manually filter the list of categories. One way I can think of to do this:
  
  1. Get all selected categories of the entry.
  2. Filter out all categories where one of the other categories is a descendent of itself.
  
  Something like this should work (untested, might need some adjustments):
  
  ```
  {% set categories = item.categoriaDir.all() %}
  {% set categoryIds = categories|column('id') %}
  {% set filteredCategories = categories|filter(
      cat => cat.getDescendants().id(categoryIds).exists()
  ) %}
  
  ```
  
  Note that this suffers from the [N+1 problem](https://craftcms.com/docs/3.x/dev/eager-loading-elements.html), so you'll want to cache this part of the template.
  
  > **Comments:**
  > **username_0**: Thanks for your response Moritz, i will check it out. Yesterday night some idea came to me: since only last category in the hierarchy will be used from the admin users to relate, they wont use for example a category level 1 that have childs options, maybe i can filter the category list to categories that DON´T HAVE DESCENDANTS, because if it has it won´t be selected, make sense? So case 1: selected category level 1: don´t have descendants. Case 2: selected category level 2: category level 1 have descendant, i must avoid it  I´m checking LEAVES  
  > **username_0**: You answer point me in the right direction, leaves do the job in my case, thank you!  
  
  ---
  
  