<p>Building on <a href="https://craftcms.stackexchange.com/questions/28063/craft-3-query-children-of-sub-categories#comment36550_28063">Robin's comment</a>, the <a href="https://docs.craftcms.com/v3/dev/element-queries/category-queries.html#parameters" rel="nofollow noreferrer">ancestorOf</a> parameter expects either an element ID or the actual element as a parameter.</p>

<p>For example, if you know the ID of the <code>fruit</code> category is <code>3</code>, you'd use <code>.anscestorOf(3)</code>, or if you already had the fruit category element, you'd do like so:</p>

<pre><code>{% set fruitCategory = craft.categories.id(3).one() %}

{% set categories = craft.categories()
    .group('food')
    .ancestorOf(fruitCategory)
    .all() %}
</code></pre>

CC BY-SA 4.0
2018-11-04T19:00:10.563Z
Thank for getting back to me.

I tried this and its still not working. So I dumped out "fruitCategory" and i was getting this returned...

/home/vagrant/code/craft/vendor/twig/twig/lib/Twig/Extension/Debug.php:50:null

any ideas?

As a result it just returns everything
Josh Mustafa
CC BY-SA 4.0
2018-10-16T00:17:18.147Z
2018-10-16T00:17:18.147Z
Brad Bell
<p><code>.ancestorOf(fruitCategory)</code> would be going up the structure. The ancestor of Fruit in this example is Food. If you want the children of Fruit you'd use <code>.decendantOf(fruitCategory)</code></p>

<p><a href="https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#descendantof" rel="nofollow noreferrer">https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#descendantof</a></p>

CC BY-SA 4.0
2020-04-30T19:45:06.003Z
2020-04-30T21:09:41.067Z
2020-04-30T21:09:41.067Z
Bill Bushee
<p>Just wondering how to only query the children on subcategories. For example...</p>

<pre><code>Food
├── Fruit
│   ├── Apples
│   ├── Bananas
│   └── Oranges
└── Vegetables
    ├── Brussels sprouts
    ├── Carrots
    └── Celery
</code></pre>

<p>I only want to query the children directly of fruit without having to get all of the food then have to loop around. As this isn't the best practice.</p>

<p>I thought it would be something like ...</p>

<pre><code>{% set categories = craft.categories()
.group('food')
.ancestorOf('fruit')
.all() %}
</code></pre>

<p>Thanks in advance.</p>

CC BY-SA 4.0
2018-10-13T17:41:21.443Z
The ancestor of expects and element or the ID of an element, not a string. You should pass the proper parameter
Robin Schambach
<craft3><categories>
Craft 3 query children of sub categories
# Craft 3 query children of sub categories

(Asked by: username_0 on 2018-10-13)

Just wondering how to only query the children on subcategories. For example...
Food
├── Fruit
│   ├── Apples
│   ├── Bananas
│   └── Oranges
└── Vegetables
    ├── Brussels sprouts
    ├── Carrots
    └── Celery

I only want to query the children directly of fruit without having to get all of the food then have to loop around. As this isn't the best practice.
I thought it would be something like ...
{% set categories = craft.categories()
.group('food')
.ancestorOf('fruit')
.all() %}

Thanks in advance.

**Tags:** craft3, categories
> **Comments:**
> **username_1**: The ancestor of expects and element or the ID of an element, not a string. You should pass the proper parameter  

---

## 2 answers

### Answer by username_2 on 2018-10-16. Score: 3

Building on Robin's comment, the ancestorOf parameter expects either an element ID or the actual element as a parameter.
For example, if you know the ID of the fruit category is 3, you'd use .anscestorOf(3), or if you already had the fruit category element, you'd do like so:
{% set fruitCategory = craft.categories.id(3).one() %}

{% set categories = craft.categories()
    .group('food')
    .ancestorOf(fruitCategory)
    .all() %}

> **Comments:**
> **username_0**: Thank for getting back to me.  I tried this and its still not working. So I dumped out "fruitCategory" and i was getting this returned...  /home/vagrant/code/craft/vendor/twig/twig/lib/Twig/Extension/Debug.php:50:null  any ideas?  As a result it just returns everything  

---

### Answer by username_3 on 2020-04-30. Score: 1

`.ancestorOf(fruitCategory)` would be going up the structure. The ancestor of Fruit in this example is Food. If you want the children of Fruit you'd use `.decendantOf(fruitCategory)`

<https://docs.craftcms.com/v3/dev/element-queries/entry-queries.html#descendantof>


---

