<p>Some of your steps are unclear, but this should get you started.</p>
<pre><code>{% for category in craft.categories.all() %}
    {{ category.title }}
    {{ category.url }}

    {% set entry = craft.entries
        .relatedTo(category)
        .orderBy('myField1 ASC')
        .one() %}
    {% if entry %}
        {% set image = entry.myField2.one() %}
        {% if image %}
            {{ image.url }}
        {% endif %}
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2022-01-16T18:12:19.073Z
Thanks for this @ben! It still doesn't seem to quite work. It throws "Impossible to access an attribute ("titleCover") on a null variable."

{% set myCategoryQuery = craft.categories().group('authors').orderBy('sortName ASC') %}

{% set categories = myCategoryQuery.all() %}

 {% for category in categories %}
    {% set entry = craft.entries
        .relatedTo(category)
        .orderBy('titlePublicationDate ASC')
        .one() %}
    {% set image = entry.titleCover.one() %}
     {{ category.url }}
     {{ image.url }} {{ image.altText }}
     {{ category.title }}
  {% endfor %}
Ben
CC BY-SA 4.0
2022-01-17T18:05:49.233Z
Any help would be greatly appreciated.
Ben
CC BY-SA 4.0
2022-01-17T20:19:17.017Z
I updated my answer with some extra conditions to ensure the entry and image are defined before outputting their properties.
Ben Croker
CC BY-SA 4.0
2022-01-18T06:38:54.757Z
Try adding `.titleCover(':notempty:')` to the entry query.
Clive Portman
CC BY-SA 4.0
2022-01-15T09:38:52.610Z
2022-01-17T20:18:45.473Z
2022-01-17T20:18:45.473Z
Ben Croker
<p>I need help. I am creating a category listing which I have working great. The next step is where I'm struggling a bit as I can't seem to query the related entries on a loop basis.</p>
<ol>
<li>For each &quot;category in categories&quot;</li>
<li>Query Entries that are related to current category, ordered by a myField1 ASC, Only One Entry</li>
<li>Set Image Variable to myField2</li>
<li>Use Image Variable with current category title and URL and related entry image</li>
<li>Loop until out of categories.</li>
</ol>
<p>Any help would be greatly appreciated.</p>

<entries><categories><query>
Find First Entry That Matches Current Category
# Find First Entry That Matches Current Category

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

I need help. I am creating a category listing which I have working great. The next step is where I'm struggling a bit as I can't seem to query the related entries on a loop basis.

1. For each "category in categories"
2. Query Entries that are related to current category, ordered by a myField1 ASC, Only One Entry
3. Set Image Variable to myField2
4. Use Image Variable with current category title and URL and related entry image
5. Loop until out of categories.

Any help would be greatly appreciated.

**Tags:** entries, categories, query


---

## 1 answers

### Answer by username_1 on 2022-01-15. Score: 3

Some of your steps are unclear, but this should get you started.
{% for category in craft.categories.all() %}
    {{ category.title }}
    {{ category.url }}

    {% set entry = craft.entries
        .relatedTo(category)
        .orderBy('myField1 ASC')
        .one() %}
    {% if entry %}
        {% set image = entry.myField2.one() %}
        {% if image %}
            {{ image.url }}
        {% endif %}
    {% endif %}
{% endfor %}

> **Comments:**
> **username_0**: Thanks for this @ben! It still doesn't seem to quite work. It throws "Impossible to access an attribute ("titleCover") on a null variable."  {% set myCategoryQuery = craft.categories().group('authors').orderBy('sortName ASC') %}  {% set categories = myCategoryQuery.all() %}   {% for category in categories %}     {% set entry = craft.entries         .relatedTo(category)         .orderBy('titlePublicationDate ASC')         .one() %}     {% set image = entry.titleCover.one() %}      {{ category.url }}      {{ image.url }} {{ image.altText }}      {{ category.title }}   {% endfor %}  
> **username_0**: Any help would be greatly appreciated.  
> **username_1**: I updated my answer with some extra conditions to ensure the entry and image are defined before outputting their properties.  
> **username_2**: Try adding `.titleCover(':notempty:')` to the entry query.  

---

