<p>Rather than preparing multiple queries - you can execute the <code>Query</code> in your field value </p>

<pre><code>{% for category in craft.categories.group('categoryGroup').level(1).find() %}
    {% for resource in category.fieldHandle.find() %}
        {{ resources.title }}
    {% endfor %}
{% endfor %
</code></pre>

CC BY-SA 4.0
2018-08-03T14:58:45.107Z
not following. i get that it cuts down on queries but it doesn't solve my problem...
strandall
CC BY-SA 4.0
2018-08-03T15:10:32.587Z
It should since you'll only fetch the related assets and not all of the hierarchy
Robin Schambach
CC BY-SA 4.0
2018-08-03T15:19:16.670Z
hm, okay. the categories only have title fields, and it's throwing an error about invoking "find" on a string
strandall
CC BY-SA 4.0
2018-08-03T15:23:55.470Z
Okay, then your initial code wasn't really good since you want to avoid the syntax you used in that case. However, categories don't work that way. You either need to create a custom field or use entries for that purpose since categories will relate all the way up to all hierarchy levels. When you relate a child, the parent will be related as well
Robin Schambach
CC BY-SA 4.0
2018-08-01T18:05:42.307Z
2018-08-01T18:05:42.307Z
Robin Schambach
<p>I've got a few categories set up with a few child categories for each. Each one has assets (PDFs) assigned to them, by going into the assets, double clicking, and setting the category field there. So for example:</p>

<pre><code>Category 1
-- asset 1
- Sub Category 1
-- asset 2
-- asset 3

Category 2
-- asset 4
- Sub Category 1
-- asset 5
-- asset 6
</code></pre>

<p>But when I'm looping through the related assets, using:</p>

<pre><code> {% for category in craft.categories.group('categoryGroup').level(1) %}
    {% set resources = craft.assets.relatedTo(category).find() %}
      {% for resource in resources %}
        {{ resources.title }}
      {% endfor %}
 {% endfor %
</code></pre>

<p>I get: </p>

<pre><code>Category 1
- asset 1
- asset 2
- asset 3
- Sub Category 1
-- asset 1
-- asset 2
-- asset 3

Category 2
- asset 4
- asset 5
- asset 6
- Sub Category 1
-- asset 4
-- asset 5
-- asset 6
</code></pre>

<p>Which logically makes sense, that it's returning all assets related to the children categories, since those are also related to the parent. But how can I exclude them from the loop, so I only get back the assets that are directly related?</p>

<assets><categories><relations><relatedto>
How to exclude child elements when looping through categories?
# How to exclude child elements when looping through categories?

(Asked by: username_0 on 2018-08-01)

I've got a few categories set up with a few child categories for each. Each one has assets (PDFs) assigned to them, by going into the assets, double clicking, and setting the category field there. So for example:
Category 1
-- asset 1
- Sub Category 1
-- asset 2
-- asset 3

Category 2
-- asset 4
- Sub Category 1
-- asset 5
-- asset 6

But when I'm looping through the related assets, using:
 {% for category in craft.categories.group('categoryGroup').level(1) %}
    {% set resources = craft.assets.relatedTo(category).find() %}
      {% for resource in resources %}
        {{ resources.title }}
      {% endfor %}
 {% endfor %

I get: 
Category 1
- asset 1
- asset 2
- asset 3
- Sub Category 1
-- asset 1
-- asset 2
-- asset 3

Category 2
- asset 4
- asset 5
- asset 6
- Sub Category 1
-- asset 4
-- asset 5
-- asset 6

Which logically makes sense, that it's returning all assets related to the children categories, since those are also related to the parent. But how can I exclude them from the loop, so I only get back the assets that are directly related?

**Tags:** assets, categories, relations, relatedto


---

## 1 answers

### Answer by username_1 on 2018-08-01. Score: 1

Rather than preparing multiple queries - you can execute the Query in your field value 
{% for category in craft.categories.group('categoryGroup').level(1).find() %}
    {% for resource in category.fieldHandle.find() %}
        {{ resources.title }}
    {% endfor %}
{% endfor %

> **Comments:**
> **username_0**: not following. i get that it cuts down on queries but it doesn't solve my problem...  
> **username_1**: It should since you'll only fetch the related assets and not all of the hierarchy  
> **username_0**: hm, okay. the categories only have title fields, and it's throwing an error about invoking "find" on a string  
> **username_1**: Okay, then your initial code wasn't really good since you want to avoid the syntax you used in that case. However, categories don't work that way. You either need to create a custom field or use entries for that purpose since categories will relate all the way up to all hierarchy levels. When you relate a child, the parent will be related as well  

---

