1

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!

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

  • 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 Jan 30 '18 at 10:23
  • 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 Jan 30 '18 at 10:40
  • 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 Feb 6 '18 at 13:19
  • 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 Feb 6 '18 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.