Please help me build my sandwich locator maker :)

  • I have 3 categories, breads, meats, condiments
  • I have a Section called subShops with a matrix field.
  • My matrix field is called sandwichBuilder, fields: breads, meats, condiments
  • This matrix field is my entry I use for building the sandwiches that the suShops offer.

I want to keep my Categories to make it easy for structure and searching.

How can I build my query so that I can find all entries using a category of bread (over 200 sub categories for bread)? I want the ability to only show those subShops that carry a certain breads based on URL segments (http://example.com/breads/spelt).

Basically, this above url example would list all the subShops that carry spelt bread, so it would display each group including - subShop, bread and typical meats that go best with spelt.

I have tried various ways and many hours trying to figure it out but with unusual results. My code below is my baseline, but need some help from anyone willing to offer examples. This is a pet project and I do plan on sharing the code when complete.

{% set entries = craft.entries.relatedTo(sanwiches).all() %}

{% for entry in entries %}
{{ entry.title }}

{% for row in entry.matrixA %}
 {% for option in row.matrixB %}
    {{ option }}
    {% endfor %}
 {% endfor %}
{% endfor %}

1 Answer 1


You have to insert your matrix field into the rated to param like here

{% set entries = craft.entries.section('subShops').relatedTo({
    targetElement: sandwich, 
    field: 'sandwichBuilder.breads' 
}) %}
  • My setup is a bit different. My Matrix field is a categories field. So in my entry matrix field I am selecting multiple categories. I need to list out the entry and all the category selections that go with that matrix field. I hope I am making sense as I am trying to understand this all myself.
    – Vin
    Apr 7, 2018 at 8:54
  • It does not matter what you select. These are all elements, there is no difference between entries and cetegories since they extend the same baseRelationField that selects elements. Craft can only restrict the elements type you are able to choose from but you could insert entries as well as categories or users or whatever you want Apr 7, 2018 at 12:31
  • Ok I get it, with Craft it's semantics, one thing over another does not matter for performance like having categories or entries. I am trying to pull records and assign them to their parent record. So thinking having an entry and intersecting with 3 categories may be to complex. For example, I wanted an entry to be the Sub Shop and separate categories to be bread, meat an condiments. I wanted to do a search from the frontend and match bread and meat combo and show the underlying condiments for each sandwich in the record.
    – Vin
    Apr 7, 2018 at 19:15
  • Yes you can do so with the related to parameter. I don't see any issue. That's the strength of Craft . You can create complex relationships between elements without problems. Relating 3+ categories with an entry and search for them is no big deal Apr 7, 2018 at 20:48
  • 1
    Sandwich is not an entry source... It's an element. Like I already said it does not matter what you insert. You can pass users, as well as categories, matrix fields, tags or whatever elements you want. If you only have one entry what's the point of searching for it anyway? This was your question wasn't it "How can I build my query so that I can find all entries using a category of bread" Apr 8, 2018 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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