In this example, I'm trying to show entries with the category "Beer". This works:

{% set myCategory = craft.categories.slug('beer') %}
 {% if entry.relatedDrinks.relatedTo(myCategory)|length %}
  {% for beer in entry.relatedDrinks.relatedTo(myCategory) %}
   {{beer.title }}
  {% endfor %}
{% endif %}

I also want to limit these entries to a certain year (not defined by Entry Date). I've created a Section called "Product Year" with entries such as "2020" and "2021". Within the entry for the beer, I've added an Entries field to select the year defined in the Product Year section. I'm not sure how to use that Product Year relation as a filter.

I'm essentially trying to say {% if entry.relatedDrinks.relatedTo(myCategory)|length "and product year is 2020 "%}


This is not a full fletched solution, but might kickstart you.

One solution would be to add a "vintage" or "brewYear" date field to your entry's section. In twig, you can then use the contents of this field to do you further selections.

Another solution would be to use a second category group containing the years you need. Then, adding another category field (for the years) to the beer section would allow you to relate the entry to this category and subsequently in Twig you are able to filter based on this relation.

If you checkout the documentation for Craft 3 relations, you might see how you can query entry records based on multiple relations.

There is also a demo website on http://barkeeper.parhelia.ch which is based on code published by Pixel & Tonic here. Its not a 100% fit for what you try to achieve, but has some nice concepts – although it also uses the SuperTable plugin for the drink incredients.

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.