Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-03T14:58:45.107Z
Answers.Text: not following. i get that it cuts down on queries but it doesn't solve my problem...
Answers.UserDisplayName: strandall
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-03T15:10:32.587Z
Answers.Text: It should since you'll only fetch the related assets and not all of the hierarchy
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-03T15:19:16.670Z
Answers.Text: hm, okay. the categories only have title fields, and it's throwing an error about invoking "find" on a string
Answers.UserDisplayName: strandall
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-03T15:23:55.470Z
Answers.Text: 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
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-01T18:05:42.307Z
Answers.LastActivityDate: 2018-08-01T18:05:42.307Z
Answers.UserDisplayName: Robin Schambach
Body: <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>
  
Tags: <assets><categories><relations><relatedto>
Title: How to exclude child elements when looping through categories?
ThreadText: # 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  
  
  ---
  
  