I hope someone has an example or something similar they have done in Craft before. Her is what I have.


Section -> Flight Company

Matrix Name -> Plane Details

Matrix Block: Plane Model and Part

Matrix Fields -> Model, Part (field type: Category)

I am needing to perform this match example (preferable in crafts.entries query)before looping through the records.

Example match needed:

Model = Cessna

Part = Propx45

I need to match both those conditions before looping through the results later on in the page. I need to make sure all the Flight Company's match that criteria before outputing the Title of the Flight Company.

How best to accomplish this?

  • Just to be clear, both Model and Part are 2 separate Categories fields inside of the Matrix field "Degree Programs"? Is there only one Matrix Block in that field?
    – Brad Bell
    May 18, 2018 at 21:33
  • Sorry about that, I had Degree Programs by mistake, it's Plane Details. But yes, they are both inside one Matrix Block, updated with the correct structure of that Matrix Field.
    – Vin
    May 18, 2018 at 22:49

1 Answer 1


Updated Answer

Turns out the original answer only works if you've got an single instance of a Matrix Block on the entry. If you have multiple, it will search through all of them and return results for any block that has any of the relations you're looking for, in spite of the inclusion of and.

As of the next post-3.0.9 release, you'll be able to get relational matches from individual Matrix Block instances using a method like so:

{% set targetModel = craft.categories.id(5).one() %}
{% set targetPart = craft.categories.id(20).one() %}

{# be sure and set the field ID of the Matrix field you're interested in here %}
{% set entryIds = craft.matrixBlocks.fieldId(55).relatedTo(['and',
        { targetElement: targetModel, field: 'model' },
        { targetElement: targetPart, field: 'part' },

{% set entries = craft.entries.id(entryIds).all() %}

{# display the results #}
Total: {{ entries|length }}<br>

    {% for entry in entries %}
        <li>{{ entry.title }}</li>
    {% endfor %}

Original Answer Assuming the setup in your question, something like this should work:

{# These are just two examples, but get the relevant target model and part categories however you want #}

{# resolve to the "Cessna" category #}
{% set targetModel = craft.categories.id(4).one() %}

{# resolve to the "Propx45" category #}
{% set targetPart = craft.categories.id(6).one() %}

{% set entries = craft.entries.section('flightCompany').relatedTo(['and',
    { targetElement: targetModel, field: 'planeDetails.model' },
    { targetElement: targetPart, field: 'planeDetails.part' },
]).all() %}

{# display the results #}
Total: {{ entries|length }}<br>

    {% for entry in entries %}
        <li>{{ entry.title }}</li>
    {% endfor %}

Here's the relevant "relations" part of the docs: https://docs.craftcms.com/v3/relations.html

  • I did all that, made sure it was right and get 0 results. I get the correct value from targetModel & targetPart but still no results. Triple check everything. Back when I started to look for an answer, the one you provided here I tried and got zero then too, so not sure why it's not working.
    – Vin
    May 18, 2018 at 23:49
  • Also to let you know some of my records have apostrophes in them, if that makes a difference.
    – Vin
    May 18, 2018 at 23:55
  • Must be something else affecting it then. If you want to send a database dump over to [email protected] we can test.
    – Brad Bell
    May 19, 2018 at 1:20
  • I will do, will be good to have this working eventually. I’ve been working at this for close to 2 weeks.
    – Vin
    May 19, 2018 at 1:22

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.