<p>Since Apples and Bananas are children of Fruit, all entires that are related to Apples or Bananas are also related to Fruit, which is why you are getting the the results you are getting. If you need to exclude entries related to the subcategories, you can do something like this:</p>

<pre><code>{% set dontInclude = [] %}

{% for child in craft.categories.descendantOf(category) %}
    {% set dontInclude = dontInclude|merge(craft.entries.relatedTo(child).ids) %}
{% endfor %}

{% set str = dontInclude|join(', not ') %}

{% set fruitEntries =  craft.entries({
    relatedTo: category,
    id: 'and, not '~str
}) %}
</code></pre>

<p>The above code will loop through Fruit's categories (Apple, Banana) and retrieve all the related entires and merging them into one array. Using the expanded <code>craft.entries</code>, you can exclude the entries related to Apple or Banana using the <code>id: 'and, not'</code> code. Hope this helps!</p>

CC BY-SA 3.0
2015-08-18T10:49:06.580Z
Awesome, that worked! Do you know if craft 3 will make this any easier?
Mark Gifford
CC BY-SA 3.0
2015-08-18T02:34:36.317Z
2015-08-18T02:34:36.317Z
Aaron Berkowitz
<p>I need to get entries which are only in current category, not child categories. </p>

<p>So if I have a category structured like this:</p>

<pre><code>Food
----Fruit
-------Apples
-------Bananas
</code></pre>

<p>I've got a categories template and I'm on the page for the Fruit category I want to know whether there's any entries just in the Fruit category and not all the entries in the Apples and Bananas categories.</p>

<p>Current I'm doing this </p>

<pre><code> craft.entries.relatedTo(category)
</code></pre>

<p>But I'm getting all the entries from the children categories as well, how do I get only the entries related to this specific category not child categories?</p>

<entries><categories><relations>
How to get entries related to current category only (not children)
# How to get entries related to current category only (not children)

(Asked by: username_0 on 2015-08-17)

I need to get entries which are only in current category, not child categories. 
So if I have a category structured like this:
Food
----Fruit
-------Apples
-------Bananas

I've got a categories template and I'm on the page for the Fruit category I want to know whether there's any entries just in the Fruit category and not all the entries in the Apples and Bananas categories.
Current I'm doing this 
 craft.entries.relatedTo(category)

But I'm getting all the entries from the children categories as well, how do I get only the entries related to this specific category not child categories?

**Tags:** entries, categories, relations


---

## 1 answers

### Answer by username_1 on 2015-08-18. Score: 8 | Accepted answer

Since Apples and Bananas are children of Fruit, all entires that are related to Apples or Bananas are also related to Fruit, which is why you are getting the the results you are getting. If you need to exclude entries related to the subcategories, you can do something like this:
{% set dontInclude = [] %}

{% for child in craft.categories.descendantOf(category) %}
    {% set dontInclude = dontInclude|merge(craft.entries.relatedTo(child).ids) %}
{% endfor %}

{% set str = dontInclude|join(', not ') %}

{% set fruitEntries =  craft.entries({
    relatedTo: category,
    id: 'and, not '~str
}) %}

The above code will loop through Fruit's categories (Apple, Banana) and retrieve all the related entires and merging them into one array. Using the expanded craft.entries, you can exclude the entries related to Apple or Banana using the id: 'and, not' code. Hope this helps!

> **Comments:**
> **username_0**: Awesome, that worked! Do you know if craft 3 will make this any easier?  

---

