Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-06T10:08:09.640Z
Answers.Text: 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. ;)
Answers.UserDisplayName: Douglas McDonald
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-06T10:12:21.000Z
Answers.Text: That's the reason!! I thought someone hijacked your account, @Douglas. :D
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-07T01:03:21.953Z
Answers.Text: haha. thanks guys. got me out of a bind ;)
Answers.UserDisplayName: Jered Masters
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-06T08:42:35.963Z
Answers.LastActivityDate: 2014-11-06T08:48:14.367Z
Answers.LastEditDate: 2014-11-06T08:48:14.367Z
Answers.UserDisplayName: carlcs
Body: <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>
  
Tags: <templating><assets><filter><category>
Title: Filter assets by a single category?
ThreadText: # 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](http://buildwithcraft.com/docs/templating/categorymodel) of that category you want to filter by. Use [`slug`](http://buildwithcraft.com/docs/templating/craft.categories) as a parameter and the `first()` method to get the first and only category matching that criteria.
  
  This CategoryModel is the [target element](http://buildwithcraft.com/docs/relations#the-relatedTo-param) of your relation. Set your [`craft.assets`](http://buildwithcraft.com/docs/templating/craft.assets#parameters) ElementCriteriaModel using the [`relatedTo`](http://buildwithcraft.com/docs/relations#the-relatedTo-param) 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`](http://buildwithcraft.com/docs/templating/craft.assets#sourceId). Set all [other parameters](http://buildwithcraft.com/docs/templating/craft.assets#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, @username_2. :D  
  > **username_0**: haha. thanks guys. got me out of a bind ;)  
  
  ---
  
  