<p>I’m assuming you want to do this in Twig rather than PHP, since you haven’t specified nor have you provided any code.</p>
<p>First, fetch all assets, eager-loading the categories.</p>
<pre><code>{% set assets = entry.product-docs
    .with('category-field-handle')
    .all()
%}
</code></pre>
<p>Then use the <a href="https://craftcms.com/docs/4.x/dev/filters.html#group" rel="nofollow noreferrer"><code>group</code></a> filter to group the assets by the category title.</p>
<pre><code>{% set assetGroups = assets|group(asset =&gt; asset.category-field-handle.one().title) %}
</code></pre>
<p>Finally, output the assets, grouped by category title:</p>
<pre><code>{% for categoryTitle, assetGroup in assetGroups %}
    {{ categoryTitle }}

    {% for asset in assetGroup %}
        {{ asset.title }}
    {% endfor %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2023-02-27T17:42:39.993Z
A thousand thanks, Ben - worked perfectly! Only minor change was to use asset.category-field-handle[0] instead of .one().
Roi Agneta
CC BY-SA 4.0
2023-02-27T17:55:16.667Z
`.one()` should work fine in Craft 4.
Ben Croker
CC BY-SA 4.0
2023-02-27T18:02:59.590Z
Good to know - in the process of moving the site from Craft 3 to Craft 4. Thanks again, Ben - much appreciated!
Roi Agneta
CC BY-SA 4.0
2023-02-26T01:04:24.933Z
2023-02-26T01:04:24.933Z
Ben Croker
<p>I have a section called “products” that has an asset field called “product-docs”. The assets are pdf files that have a Category field. Each product can have many different types of documents; to make it easier to find the document you want (on a product page), there are five different Document Categories, each of which has a number of Document Types, e.g.:</p>
<p>Installation Guides (Document Category)
Mounting instructions (Document Type)
Wiring Instructions (Document Type)</p>
<p>What I want to do is to show all documents for a given product, grouped by Document Category. If I fetch the data for the document category field it returns a string that comprises both the Document Category and the Document Sub Category. Here is an example of what I am trying to accomplish: <a href="https://www.ruskin.com/model/aiq-td-50" rel="nofollow noreferrer">https://www.ruskin.com/model/aiq-td-50</a> (docs are grouped by category in the section labelled Additional Resources)</p>
<p>How can I query the category field so that I can access the Document Category and then all the related Sub Categories? I tried using .level1, but that does not work.</p>

<categories><group>
How to group assets by category/sub-category
# How to group assets by category/sub-category

(Asked by: username_0 on 2023-02-26)

I have a section called “products” that has an asset field called “product-docs”. The assets are pdf files that have a Category field. Each product can have many different types of documents; to make it easier to find the document you want (on a product page), there are five different Document Categories, each of which has a number of Document Types, e.g.:

Installation Guides (Document Category)
Mounting instructions (Document Type)
Wiring Instructions (Document Type)

What I want to do is to show all documents for a given product, grouped by Document Category. If I fetch the data for the document category field it returns a string that comprises both the Document Category and the Document Sub Category. Here is an example of what I am trying to accomplish: <https://www.ruskin.com/model/aiq-td-50> (docs are grouped by category in the section labelled Additional Resources)

How can I query the category field so that I can access the Document Category and then all the related Sub Categories? I tried using .level1, but that does not work.

**Tags:** categories, group


---

## 1 answers

### Answer by username_1 on 2023-02-26. Score: 3 | Accepted answer

I’m assuming you want to do this in Twig rather than PHP, since you haven’t specified nor have you provided any code.
First, fetch all assets, eager-loading the categories.
{% set assets = entry.product-docs
    .with('category-field-handle')
    .all()
%}

Then use the group filter to group the assets by the category title.
{% set assetGroups = assets|group(asset => asset.category-field-handle.one().title) %}

Finally, output the assets, grouped by category title:
{% for categoryTitle, assetGroup in assetGroups %}
    {{ categoryTitle }}

    {% for asset in assetGroup %}
        {{ asset.title }}
    {% endfor %}
{% endfor %}

> **Comments:**
> **username_0**: A thousand thanks, Ben - worked perfectly! Only minor change was to use asset.category-field-handle[0] instead of .one().  
> **username_1**: `.one()` should work fine in Craft 4.  
> **username_0**: Good to know - in the process of moving the site from Craft 3 to Craft 4. Thanks again, Ben - much appreciated!  

---

