Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-16T18:12:19.073Z
Answers.Text: 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 %}
Answers.UserDisplayName: Ben
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-17T18:05:49.233Z
Answers.Text: Any help would be greatly appreciated.
Answers.UserDisplayName: Ben
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-17T20:19:17.017Z
Answers.Text: I updated my answer with some extra conditions to ensure the entry and image are defined before outputting their properties.
Answers.UserDisplayName: Ben Croker
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-18T06:38:54.757Z
Answers.Text: Try adding `.titleCover(':notempty:')` to the entry query.
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-01-15T09:38:52.610Z
Answers.LastActivityDate: 2022-01-17T20:18:45.473Z
Answers.LastEditDate: 2022-01-17T20:18:45.473Z
Answers.UserDisplayName: Ben Croker
Body: <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>
  
Tags: <entries><categories><query>
Title: Find First Entry That Matches Current Category
ThreadText: # 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.  
  
  ---
  
  