TransWikia.com

How do I display entries depending on multiple relations?

Craft CMS Asked by jamesd on May 9, 2021

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 %}
  <h1>Beers</h1>
  {% 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 "%}

One Answer

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.

Answered by MisterMike on May 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP