I am trying to load entries from a section which have a category field with multiple values. Based on that, I want to show specific entries filtered by category.

Example: I have a news section and I want to load sports and entertainment news only.

Help me on this, please.


You could use this, note: this is untested maybe you have to remove the first() or use targetElement: category1.id

{% set category1 = craft.categories.id(yourId).one() %}
{% set category2 = craft.categories.id(yourId2).one() %}
{% set news = craft.entries.all({
    section: 'news',
    relatedTo: [
        { targetElement: category1.id },
        { targetElement: category2.id }
}) %}

You should read about that topic here click

  • I think you can pass the ID's of the categories directly into the related to parameter and add the source attribute pointed to the category field as well. Makes for a few lines less code. – Tom De Smet Nov 13 '17 at 10:42
  • Thanks it works, Can I pass array in targetElement? – Kantesh Hotchandani Nov 13 '17 at 10:45
  • Done some thing like this way {% set entries = craft.entries({ section: 'news', orderBy: 'postDate desc', limit:3, relatedTo: [ 'or', { targetElement: entry.newsCategory.all() } ] }).id('not '~entry.id).all() %} – Kantesh Hotchandani Nov 13 '17 at 10:53

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.