Select query question 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!
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