<p>@carlcs helped me stumble upon a solution. Using the <code>.level(2)</code> method gave the desired result.</p>

<p><code>{% set cats =  entry.categoryInput.level(2) %}</code></p>

CC BY-SA 3.0
2015-10-28T19:17:28.587Z
2015-10-28T19:17:28.587Z
a-am
<p>You could set the <code>level</code> parameter according to your requirements:</p>

<pre><code>{% set cats = craft.categories({
    relatedTo: {
        sourceElement: entry,
        field: 'categoryInput'
    },
    level: 2,
    limit: null
}) %}
</code></pre>

CC BY-SA 3.0
2015-10-28T19:02:43.950Z
Will this work if the input is part of a matrix?
a-am
CC BY-SA 3.0
2015-10-28T19:09:33.137Z
You would then need to set field to `myMatrixField.myCategoriesField`.
carlcs
CC BY-SA 3.0
2015-10-28T19:13:56.020Z
You pointed me in the correct direction.  Using level(2) off of the input works.
a-am
CC BY-SA 3.0
2015-10-28T19:22:11.863Z
`entry.myCategoriesField` is actually just a shortcut for the full `craft.categories` criteria model I posted. So I'm surprised this solution doesn't work for you.
carlcs
CC BY-SA 3.0
2015-10-28T19:38:05.487Z
This does work but I figured there should be a quicker way than calling `craft.categories`.  When you wrote "set the level" I thought I would try using `level(2)` off of the input which worked. As you said it is returning a criteria model, which confused me as to why using `getDescendants` or `children` didn't work.
a-am
CC BY-SA 3.0
2015-10-28T18:49:38.080Z
2015-10-28T18:49:38.080Z
carlcs
<p>I have a category input. The category group has two levels. </p>

<p><a href="https://i.sstatic.net/kUdoy.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kUdoy.png" alt="enter image description here"></a></p>

<p>When accessing the category input via Twig I am trying to only grab sub categories the user selected.  However when I use </p>

<pre><code>{% set firstCat = entry.categoryInput.first %}
{% set cats = firstCat.getDescendants() %}
</code></pre>

<p>or <code>children</code> it grabs all the descendants from <em>Categories > Group</em> not just from the structure that was selected from the input.  </p>

<templating><categories><elementcriteriamodel>
Use only the sub categories from a category input
# Use only the sub categories from a category input

(Asked by: username_0 on 2015-10-28)

I have a category input. The category group has two levels. 

When accessing the category input via Twig I am trying to only grab sub categories the user selected.  However when I use 
{% set firstCat = entry.categoryInput.first %}
{% set cats = firstCat.getDescendants() %}

or children it grabs all the descendants from Categories > Group not just from the structure that was selected from the input.

**Tags:** templating, categories, elementcriteriamodel

---

## 2 answers

### Answer by username_0 on 2015-10-28. Score: 0 | Accepted answer

@carlcs helped me stumble upon a solution. Using the `.level(2)` method gave the desired result.

`{% set cats = entry.categoryInput.level(2) %}`


---

### Answer by username_1 on 2015-10-28. Score: 2

You could set the level parameter according to your requirements:
{% set cats = craft.categories({
    relatedTo: {
        sourceElement: entry,
        field: 'categoryInput'
    },
    level: 2,
    limit: null
}) %}

> **Comments:**
> **username_0**: Will this work if the input is part of a matrix?  
> **username_1**: You would then need to set field to `myMatrixField.myCategoriesField`.  
> **username_0**: You pointed me in the correct direction.  Using level(2) off of the input works.  
> **username_1**: `entry.myCategoriesField` is actually just a shortcut for the full `craft.categories` criteria model I posted. So I'm surprised this solution doesn't work for you.  
> **username_0**: This does work but I figured there should be a quicker way than calling `craft.categories`.  When you wrote "set the level" I thought I would try using `level(2)` off of the input which worked. As you said it is returning a criteria model, which confused me as to why using `getDescendants` or `children` didn't work.  

---

