<p>You first have to get the <a href="http://buildwithcraft.com/docs/templating/categorymodel" rel="nofollow">CategoryModel</a> of that category you want to filter by. Use <a href="http://buildwithcraft.com/docs/templating/craft.categories" rel="nofollow"><code>slug</code></a> as a parameter and the <code>first()</code> method to get the first and only category matching that criteria.</p>

<p>This CategoryModel is the <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="nofollow">target element</a> of your relation. Set your <a href="http://buildwithcraft.com/docs/templating/craft.assets#parameters" rel="nofollow"><code>craft.assets</code></a> ElementCriteriaModel using the <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="nofollow"><code>relatedTo</code></a> parameter to filter by your category and also set the relation's field name here (not necessary if you don't have other relations set on your assets).</p>

<p>If you really have the need to further filter by an asset source (don't get the assets which match the other criteria but are in a different source folder), you (currently) can only do this by looking up that asset source's ID in the database and filter by that <a href="http://buildwithcraft.com/docs/templating/craft.assets#sourceId" rel="nofollow"><code>sourceId</code></a>. Set all <a href="http://buildwithcraft.com/docs/templating/craft.assets#parameters" rel="nofollow">other parameters</a> to your needs.</p>

<pre><code>{% set categorySlug = 'sitePlan' %}

{# Get the CategoryModel #}
{% set category = craft.categories.slug(categorySlug).first() %}

{# Get the assets related to this CategoryModel #}
{% set assets = craft.assets({
    relatedTo: {
        targetElement: category,
        field: 'documentType',
    },
    sourceId: '1',
    limit: null,
    order: 'dateUpdated desc',
}) %}
</code></pre>

CC BY-SA 3.0
2014-11-06T10:08:09.640Z
Much better answer. I really shouldn't be answering craft questions after drinking all night. Mine was just plain wrong. Thanks for coming to the rescue. ;)
Douglas McDonald
CC BY-SA 3.0
2014-11-06T10:12:21.000Z
That's the reason!! I thought someone hijacked your account, @Douglas. :D
carlcs
CC BY-SA 3.0
2014-11-07T01:03:21.953Z
haha. thanks guys. got me out of a bind ;)
Jered Masters
CC BY-SA 3.0
2014-11-06T08:42:35.963Z
2014-11-06T08:48:14.367Z
2014-11-06T08:48:14.367Z
carlcs
<p>Is there a way in Craft to filter assets by an associated category?</p>

<p>In asset settings, one of our asset sources has been given a category field, and those assets have been given a category.</p>

<p>ie; asset source "documentLibrary" has category field "documentType"
and asset "documentOne" has documentType "sitePlan".</p>

<p>I have tried the following variations below:</p>

<pre><code>craft.assets({documentType: "sitePlan"})
craft.assets({documentType: "contains, sitePlan"})
craft.assets({documentType: "LIKE '%sitePlan%'"})
craft.assets({documentType: array('sitePlan')})
craft.assets({count(documentType): '&gt; 0'})
craft.assets({relatesTo: "sitePlan"})
</code></pre>

<templating><assets><filter><category>
Filter assets by a single category?
# Filter assets by a single category?

(Asked by: username_0 on 2014-11-06)

Is there a way in Craft to filter assets by an associated category?
In asset settings, one of our asset sources has been given a category field, and those assets have been given a category.
ie; asset source "documentLibrary" has category field "documentType"
and asset "documentOne" has documentType "sitePlan".
I have tried the following variations below:
craft.assets({documentType: "sitePlan"})
craft.assets({documentType: "contains, sitePlan"})
craft.assets({documentType: "LIKE '%sitePlan%'"})
craft.assets({documentType: array('sitePlan')})
craft.assets({count(documentType): '> 0'})
craft.assets({relatesTo: "sitePlan"})

**Tags:** templating, assets, filter, category

---

## 1 answers

### Answer by username_1 on 2014-11-06. Score: 2 | Accepted answer

You first have to get the CategoryModel of that category you want to filter by. Use slug as a parameter and the first() method to get the first and only category matching that criteria.
This CategoryModel is the target element of your relation. Set your craft.assets ElementCriteriaModel using the relatedTo parameter to filter by your category and also set the relation's field name here (not necessary if you don't have other relations set on your assets).
If you really have the need to further filter by an asset source (don't get the assets which match the other criteria but are in a different source folder), you (currently) can only do this by looking up that asset source's ID in the database and filter by that sourceId. Set all other parameters to your needs.
{% set categorySlug = 'sitePlan' %}

{# Get the CategoryModel #}
{% set category = craft.categories.slug(categorySlug).first() %}

{# Get the assets related to this CategoryModel #}
{% set assets = craft.assets({
    relatedTo: {
        targetElement: category,
        field: 'documentType',
    },
    sourceId: '1',
    limit: null,
    order: 'dateUpdated desc',
}) %}

> **Comments:**
> **username_2**: Much better answer. I really shouldn't be answering craft questions after drinking all night. Mine was just plain wrong. Thanks for coming to the rescue. ;)  
> **username_1**: That's the reason!! I thought someone hijacked your account, @Douglas. :D  
> **username_0**: haha. thanks guys. got me out of a bind ;)  

---

