This allows me to pull the related categories from a second category group for a given category.

{% set entries = craft.entries.relatedTo(category).find() %}
{% set tags = craft.tags.relatedTo(entries).find() %}   
{% set markets = craft.categories.group('markets').relatedTo(entries).find() %}

BUT, it pulls both the parent and descendant categories from markets. And, some markets do not have descendants - so adding `.level(2) would not capture those that only have a level(1) assigned.

I want to list only the last level assigned whether it is level(1) or level(2).

I tried adding .last() but that generates a CException error.

{% set markets = craft.categories.group('markets').last().relatedTo(entries).find() %}

What am I missing?


  • All entries have a category from markets assigned. It is mandatory.
    – Hellyar
    Mar 1 '17 at 20:03

.last is the opposite of .first, meaning that you can only apply it at the end of your query.

{% set markets = craft.categories.group('markets').relatedTo(entries).last() %}

And similar to .first, the .last method will only return a single Entry.

  • That created a template error. Impossible to access an attribute ("title") on a string variable ("8") So, is there a way to do it without using last()? The goal is to pull all the markets using their last level to create filter buttons.
    – Hellyar
    Mar 1 '17 at 20:33
  • So, I am not trying to return a single entry or any entries. See above.
    – Hellyar
    Mar 2 '17 at 19:22

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.