<p>Why don't you use the prepared <code>ElementCriteriaModel</code> in the category and change the query?</p>

<pre><code>{% for category in craft.categories.group('whatever') %}
    {% set articles = category.journalismMainCategory.limit(7).order('postDate').find() %} 
{% endfor %}
</code></pre>

<p>Your <code>category.journalismMainCategory</code> contains an <code>ElementCriteriaModel</code> that will fetch all related entries you can include a limit, change the sort order and execute it, so you'll receive 7 articles with the correct order for each of your categories</p>

<p>Edit: link to the <a href="https://craftcms.com/docs/entries-fields#templating" rel="nofollow noreferrer">docs</a> </p>

CC BY-SA 3.0
2018-01-30T10:23:01.043Z
Thanks Robin. This doesn't group the categories though - right? I need the 7 latest articles from a category group with 9 categories but each article must be from a different category (the latest for each category). This can be easily done with a group by query - but I am not sure how to implement id from the template (and not from a plugin)
Evangelos
CC BY-SA 3.0
2018-01-30T10:40:03.567Z
What do you mean with "how to implement id from the template?" You don't need to group them by category since they are already grouped by the category. You said you have related entries within your categories so when you loop through your categories and fetch those related in your field they are already grouped. So you have your list of 9 categories and the 7 related articles of those
Robin Schambach
CC BY-SA 3.0
2018-02-06T13:19:07.660Z
Thanks Robin. I cannot access entries with this {% set articles = category.journalismMainCategory.limit(7).order('postDate').find() %} it gives me an error. Eventhough I need the latest article for each category, I also need them sorted by date (when going through the categories the generated array will have the order by which the categories are read - not by postDate for their entries. I've managed to get the array I need, but then I cannot sort it by date.
Evangelos
CC BY-SA 3.0
2018-02-06T13:20:29.687Z
Here is the code `                {% set selectedCategories = [98, 100, 2412, 2408, 2410, 2414, 2416, 74400, 74401] %} {# 55332, 53333 | 74400, 74401 #}
                {% set articles = [] %}
                {% for category in craft.categories.id(selectedCategories) %}
                    {% set articles = articles|merge(craft.entries.relatedTo(category).limit(1).order('postDate desc')) %}
                {% endfor %}
                {% for item in articles %}
       {{item.id}} - {{item.title}} - {{item.postDate|date('d/m/Y')}} - {{item.journalismMainCategory.last}}<br />{% endfor %}`
Evangelos
CC BY-SA 3.0
2018-01-26T13:26:12.003Z
2018-01-26T14:45:33.330Z
2018-01-26T14:45:33.330Z
Robin Schambach
<p>I am trying to do the following with craft.entries but I think it may not be possible without a plugin.</p>

<p>I have 9 categories with related entries on all of them. I would like to get the latest articles (postDate) for each one the categories and then group by category and only show only the seven latest articles, one for each category.</p>

<p>The following works but it fetches 100 articles each time (I can reduce it but still its not ideal)</p>

<pre><code>{% set latestArticles = craft.entries.section('articles').relatedTo({element: selectedCategories, field: 'journalismMainCategory'}).order('postDate desc').find() %}
{% set groupedArticles = latestArticles|group('journalismMainCategory.first.id')|slice(0,7) %}
</code></pre>

<p>I've also tried the following but I cannot get it to sort by date or group the category properly. It retrieves the latest article for each category, but in the order of the category (and not by post date)</p>

<pre><code>{% set selectedCategories = [98, 100, 2412, 2408, 2410, 2414, 2416, 55332, 55333] %}

{% for category in selectedCategories %}
       {% set stilesArticles = stilesArticles|merge([craft.entries.section('articles').relatedTo({element: selectedCategories[loop.index0], field: 'journalismMainCategory'}).limit(1).first]) %}
{% endfor %}
</code></pre>

<p>Any ideas on how something like this can be done without a plugin?</p>

<p>Cheers!</p>

<entries><ordering><sorting><group><query>
Select query question
# Select query question

(Asked by: username_0 on 2018-01-26)

I am trying to do the following with craft.entries but I think it may not be possible without a plugin.
I have 9 categories with related entries on all of them. I would like to get the latest articles (postDate) for each one the categories and then group by category and only show only the seven latest articles, one for each category.
The following works but it fetches 100 articles each time (I can reduce it but still its not ideal)
{% set latestArticles = craft.entries.section('articles').relatedTo({element: selectedCategories, field: 'journalismMainCategory'}).order('postDate desc').find() %}
{% set groupedArticles = latestArticles|group('journalismMainCategory.first.id')|slice(0,7) %}

I've also tried the following but I cannot get it to sort by date or group the category properly. It retrieves the latest article for each category, but in the order of the category (and not by post date)
{% set selectedCategories = [98, 100, 2412, 2408, 2410, 2414, 2416, 55332, 55333] %}

{% for category in selectedCategories %}
       {% set stilesArticles = stilesArticles|merge([craft.entries.section('articles').relatedTo({element: selectedCategories[loop.index0], field: 'journalismMainCategory'}).limit(1).first]) %}
{% endfor %}

Any ideas on how something like this can be done without a plugin?
Cheers!

**Tags:** entries, ordering, sorting, group, query


---

## 1 answers

### Answer by username_1 on 2018-01-26. Score: 1

Why don't you use the prepared ElementCriteriaModel in the category and change the query?
{% for category in craft.categories.group('whatever') %}
    {% set articles = category.journalismMainCategory.limit(7).order('postDate').find() %} 
{% endfor %}

Your category.journalismMainCategory contains an ElementCriteriaModel that will fetch all related entries you can include a limit, change the sort order and execute it, so you'll receive 7 articles with the correct order for each of your categories
Edit: link to the docs

> **Comments:**
> **username_0**: Thanks Robin. This doesn't group the categories though - right? I need the 7 latest articles from a category group with 9 categories but each article must be from a different category (the latest for each category). This can be easily done with a group by query - but I am not sure how to implement id from the template (and not from a plugin)  
> **username_1**: What do you mean with "how to implement id from the template?" You don't need to group them by category since they are already grouped by the category. You said you have related entries within your categories so when you loop through your categories and fetch those related in your field they are already grouped. So you have your list of 9 categories and the 7 related articles of those  
> **username_0**: Thanks Robin. I cannot access entries with this {% set articles = category.journalismMainCategory.limit(7).order('postDate').find() %} it gives me an error. Eventhough I need the latest article for each category, I also need them sorted by date (when going through the categories the generated array will have the order by which the categories are read - not by postDate for their entries. I've managed to get the array I need, but then I cannot sort it by date.  
> **username_0**: Here is the code `                {% set selectedCategories = [98, 100, 2412, 2408, 2410, 2414, 2416, 74400, 74401] %} {# 55332, 53333 | 74400, 74401 #}                 {% set articles = [] %}                 {% for category in craft.categories.id(selectedCategories) %}                     {% set articles = articles|merge(craft.entries.relatedTo(category).limit(1).order('postDate desc')) %}                 {% endfor %}                 {% for item in articles %}        {{item.id}} - {{item.title}} - {{item.postDate|date('d/m/Y')}} - {{item.journalismMainCategory.last}}<br />{% endfor %}`  

---

