<p>Craft does not allow for this (as of Craft 3.1.34). When saving selected categories to a Category relationship field, it automatically assigns the parent categories and does nothing to designate which categories were actually selected/clicked/chosen.</p>

CC BY-SA 4.0
2019-07-24T15:41:10.783Z
2019-07-24T15:41:10.783Z
Benjamin Kohl
<p>Here's how to isolate only your <em>chosen</em> categories. This works off the premise that if a related category is a parent of one of the other related categories, it is <em>not</em> a category you chose.</p>
<pre><code>{% set relatedCategories = entry.myCategoryField.with(['parent']).all() %}
{% set parentCategories = relatedCategories | map(c =&gt; c.parent) %}
{% set chosenCategories = relatedCategories | diff(parentCategories) %}
</code></pre>

CC BY-SA 4.0
2022-11-08T18:03:52.150Z
This is a really important answer, as it allows behaviour (in perhaps a slightly hacky way) that perhaps should be core.
ianp
CC BY-SA 4.0
2020-09-11T18:31:35.207Z
2020-09-11T18:31:35.207Z
Evan Warner
<p>Unless I'm missing something - this is how you run through the categories you selected in your Category field when you're looking at that entry's template:</p>

<pre><code>{% for cat in entry.yourCategoryFieldhandle.all() %}
    {{ cat.title }}
{% endfor %}
</code></pre>

<p>EDIT: Ignore this, I can see what you mean. If you select a child category it automatically selects all the parents too.</p>

<p>This may not be suitable for your site, but if this is required functionality I'd be looking at using a Structure instead of categories. Using a Structure combined with an Entries field will let you do this no problem. P&amp;T are going to be removing Categories and Tags in Craft 4 anyway.</p>

CC BY-SA 4.0
2019-07-17T15:29:19.367Z
Yes, the issue is that we aren't trying to query the parents in this situation and we aren't necessarily working with "leaves" either.
Benjamin Kohl
CC BY-SA 4.0
2019-07-18T15:51:10.527Z
Yes, a Structure of Entries would be far more appropriate if you don't want the hierarchical inheritance.
foamcow
CC BY-SA 4.0
2019-07-24T15:36:06.323Z
The issue is that we *do* want the hierarchical inheritance when assigning the taxonomy to an object but we do not want it in other places where those taxonomies are referenced and not actually being assigned to an object. This is a design flaw in Craft—plain and simple.
Benjamin Kohl
CC BY-SA 4.0
2019-07-17T14:17:02.577Z
2019-07-17T20:12:52.493Z
2019-07-17T20:12:52.493Z
darylknight
<p>This is untested, but you could do a loop within each category to compare it and figure out if it's the end of a branch. I've probably missed something in my code but it might help as a start.</p>

<pre><code>{% set catsYouWant = [] %}
{% for cat in entry.yourCategoryFieldhandle.all() %}

    {% set possible = false %}

    // If category is level one it is possibly the end of branch
    {% if cat.level == 1 %}
        {% set possible == true %}
    {% endif %}

    // Compare category against others to see if is a parent, if so we can discount it
    {% for catCompare in entry.yourCategoryFieldhandle.all() %}

        // if catCompare is child of cat then cat is not end of branch
        {% if catCompare.ancestorOf(cat) %}
            {% set possible = false %}
        {% endif %}

    {% endfor %}

    {% if possible == true %}
        {% set catsYouWant = catsYouWant|merge(cat) %}
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2019-07-18T15:04:22.317Z
What I'm looking for in particular is a means to query the field directly to give me the categories (or IDs) that were specifically selected when populating the field. In a perfect world, this might look something like: entry.catFieldHandle.chosen().ids() In theory, something like that would return the three category IDs provided in my example. It would not return their parents or anything else.
Benjamin Kohl
CC BY-SA 4.0
2019-07-18T15:20:47.637Z
Hopefully someone else knows a cleaner way, but in theory, the code above should give you the same result albeit not as tidy.
dmatthams
CC BY-SA 4.0
2019-07-17T16:32:52.050Z
2019-07-18T15:35:06.423Z
2019-07-18T15:35:06.423Z
dmatthams
<p>Suppose we have a category group that allows for any number of levels to its structure. We also have a category field that allows for choosing any number of categories from that group.</p>

<p>In our example, we want to select three categories: one is a level 1 category in that group, another is a direct child of a different level 1 category and the third is a level 4 category from another branch in the category group (all three are in different branches for this example).</p>

<p>The question is: How do we query the category field to only return the categories we <em>selected</em>—the categories we actually clicked on when selecting categories? (Craft 3)</p>

CC BY-SA 4.0
2019-07-17T14:15:58.933Z
Do you mean you don't want to return anything related to the selected category's children?
dmatthams
CC BY-SA 4.0
2019-07-17T15:47:45.803Z
I think it's pretty clear. Here's an example: If there are 5000 categories, nested 50 deep, and content author picks 3 categories from anywhere in that category tree JUST QUERY FOR THE THREE SELECTED CATEGORIES. NO PARENTS. NO CHILDREN. NOTHING ELSE. Just what was clicked.
Ryan Masuga
CC BY-SA 4.0
2019-07-17T16:14:04.837Z
I see, this is a tricky one.
dmatthams
CC BY-SA 4.0
2019-07-18T15:06:12.537Z
There HAS to be a way to do this. The selected categories ARE the field's value, are they not?
Benjamin Kohl
<craft3><categories><relations><elementquery>
With a categories field, how do I query only the *selected* categories?
# With a categories field, how do I query only the *selected* categories?

(Asked by: username_0 on 2019-07-17)

Suppose we have a category group that allows for any number of levels to its structure. We also have a category field that allows for choosing any number of categories from that group.

In our example, we want to select three categories: one is a level 1 category in that group, another is a direct child of a different level 1 category and the third is a level 4 category from another branch in the category group (all three are in different branches for this example).

The question is: How do we query the category field to only return the categories we *selected*—the categories we actually clicked on when selecting categories? (Craft 3)

**Tags:** craft3, categories, relations, elementquery

> **Comments:**
> **username_1**: Do you mean you don't want to return anything related to the selected category's children?  
> **username_2**: I think it's pretty clear. Here's an example: If there are 5000 categories, nested 50 deep, and content author picks 3 categories from anywhere in that category tree JUST QUERY FOR THE THREE SELECTED CATEGORIES. NO PARENTS. NO CHILDREN. NOTHING ELSE. Just what was clicked.  
> **username_1**: I see, this is a tricky one.  
> **username_0**: There HAS to be a way to do this. The selected categories ARE the field's value, are they not?  

---

## 4 answers

### Answer by username_0 on 2019-07-24. Score: 1 | Accepted answer

Craft does not allow for this (as of Craft 3.1.34). When saving selected categories to a Category relationship field, it automatically assigns the parent categories and does nothing to designate which categories were actually selected/clicked/chosen.


---

### Answer by username_3 on 2020-09-11. Score: 4

Here's how to isolate only your chosen categories. This works off the premise that if a related category is a parent of one of the other related categories, it is not a category you chose.
{% set relatedCategories = entry.myCategoryField.with(['parent']).all() %}
{% set parentCategories = relatedCategories | map(c => c.parent) %}
{% set chosenCategories = relatedCategories | diff(parentCategories) %}

> **Comments:**
> **username_4**: This is a really important answer, as it allows behaviour (in perhaps a slightly hacky way) that perhaps should be core.  

---

### Answer by username_5 on 2019-07-17. Score: 0

Unless I'm missing something - this is how you run through the categories you selected in your Category field when you're looking at that entry's template:
{% for cat in entry.yourCategoryFieldhandle.all() %}
    {{ cat.title }}
{% endfor %}

EDIT: Ignore this, I can see what you mean. If you select a child category it automatically selects all the parents too.
This may not be suitable for your site, but if this is required functionality I'd be looking at using a Structure instead of categories. Using a Structure combined with an Entries field will let you do this no problem. P&T are going to be removing Categories and Tags in Craft 4 anyway.

> **Comments:**
> **username_0**: Yes, the issue is that we aren't trying to query the parents in this situation and we aren't necessarily working with "leaves" either.  
> **username_6**: Yes, a Structure of Entries would be far more appropriate if you don't want the hierarchical inheritance.  
> **username_0**: The issue is that we *do* want the hierarchical inheritance when assigning the taxonomy to an object but we do not want it in other places where those taxonomies are referenced and not actually being assigned to an object. This is a design flaw in Craft—plain and simple.  

---

### Answer by username_1 on 2019-07-17. Score: 0

This is untested, but you could do a loop within each category to compare it and figure out if it's the end of a branch. I've probably missed something in my code but it might help as a start.
{% set catsYouWant = [] %}
{% for cat in entry.yourCategoryFieldhandle.all() %}

    {% set possible = false %}

    // If category is level one it is possibly the end of branch
    {% if cat.level == 1 %}
        {% set possible == true %}
    {% endif %}

    // Compare category against others to see if is a parent, if so we can discount it
    {% for catCompare in entry.yourCategoryFieldhandle.all() %}

        // if catCompare is child of cat then cat is not end of branch
        {% if catCompare.ancestorOf(cat) %}
            {% set possible = false %}
        {% endif %}

    {% endfor %}

    {% if possible == true %}
        {% set catsYouWant = catsYouWant|merge(cat) %}
    {% endif %}
{% endfor %}

> **Comments:**
> **username_0**: What I'm looking for in particular is a means to query the field directly to give me the categories (or IDs) that were specifically selected when populating the field. In a perfect world, this might look something like: entry.catFieldHandle.chosen().ids() In theory, something like that would return the three category IDs provided in my example. It would not return their parents or anything else.  
> **username_1**: Hopefully someone else knows a cleaner way, but in theory, the code above should give you the same result albeit not as tidy.  

---

