<p>Managed to make this work, corrected a couple of things and tidied up, but the main issue was realising the field param has to be a string (duh), not a variable. My code now looks like this:</p>

<pre><code>{% set entries = craft.entries.type('artistFolio').limit(null) %}

{% set imagesInUse = craft.assets.relatedTo({
    sourceElement: entries,
    field: 'imageList'
}) %}
</code></pre>

<p>This gives me a model with all images appearing inside the Matrix field 'imageList' within the entry type 'artistFolio'. I can then search these as so:</p>

<pre><code>{% set searchTerm = craft.request.getParam('q') %}
{% set results = imagesInUse.search('*' ~ searchTerm ~ '*').order('score') %}

{% for image in results %}
    ...
{ % endfor %}
</code></pre>

CC BY-SA 3.0
2015-01-16T12:29:15.263Z
2015-01-17T16:48:56.667Z
2015-01-17T16:48:56.667Z
pumkincreative
<p>I want to run a search on Assets that are in use within a particular section. The Assets are added to entries via a Matrix field.</p>

<p>The search term is provided via a url parameter:</p>

<pre><code>{% set searchTerm = craft.request.getParam('q') %}
</code></pre>

<p>Somewhat optimistically I tried this:</p>

<pre><code>{% set imagesInUse = craft.assets.relatedTo({
  targetElement: craft.entries({
    section : 'agency'
  }),
  field : imageList.keyImage
}) %}
</code></pre>

<p>Where imageList is the Matrix field and keyImage is the Assets field. The block name is imageSpread.</p>

<p>This apparently returns nothing.</p>

<p>For the search part, I'm trying to search within 'imagesInUse':</p>

<pre><code>{% for image in imagesInUse.search('*' ~ searchTerm ~ '*').order('score') %} ... {% endfor %}
</code></pre>

<p>It looks like a relatively complex query might be required, but I'm not sure where to go from here.</p>

CC BY-SA 3.0
2015-01-16T12:02:38.823Z
I've broken this down a bit more and discovered that the field imageList is reported as not found. The field name is definitely correct. I've also updated the entry call to filter by a specific entry type, one which definitely has that field present. And finally I've tried targetElement, element and sourceElement. Clearly I'm misunderstanding something here - any help greatly appreciated.
pumkincreative
<assets><matrix><relations><sections><search>
Search Assets in Matrix fields, if they appear in a Section
# Search Assets in Matrix fields, if they appear in a Section

(Asked by: username_0 on 2015-01-15)

I want to run a search on Assets that are in use within a particular section. The Assets are added to entries via a Matrix field.
The search term is provided via a url parameter:
{% set searchTerm = craft.request.getParam('q') %}

Somewhat optimistically I tried this:
{% set imagesInUse = craft.assets.relatedTo({
  targetElement: craft.entries({
    section : 'agency'
  }),
  field : imageList.keyImage
}) %}

Where imageList is the Matrix field and keyImage is the Assets field. The block name is imageSpread.
This apparently returns nothing.
For the search part, I'm trying to search within 'imagesInUse':
{% for image in imagesInUse.search('*' ~ searchTerm ~ '*').order('score') %} ... {% endfor %}

It looks like a relatively complex query might be required, but I'm not sure where to go from here.

**Tags:** assets, matrix, relations, sections, search

> **Comments:**
> **username_0**: I've broken this down a bit more and discovered that the field imageList is reported as not found. The field name is definitely correct. I've also updated the entry call to filter by a specific entry type, one which definitely has that field present. And finally I've tried targetElement, element and sourceElement. Clearly I'm misunderstanding something here - any help greatly appreciated.  

---

## 1 answers

### Answer by username_0 on 2015-01-16. Score: 2 | Accepted answer

Managed to make this work, corrected a couple of things and tidied up, but the main issue was realising the field param has to be a string (duh), not a variable. My code now looks like this:
{% set entries = craft.entries.type('artistFolio').limit(null) %}

{% set imagesInUse = craft.assets.relatedTo({
    sourceElement: entries,
    field: 'imageList'
}) %}

This gives me a model with all images appearing inside the Matrix field 'imageList' within the entry type 'artistFolio'. I can then search these as so:
{% set searchTerm = craft.request.getParam('q') %}
{% set results = imagesInUse.search('*' ~ searchTerm ~ '*').order('score') %}

{% for image in results %}
    ...
{ % endfor %}


---

