<p><strong>Updated Answer</strong></p>

<p>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 <em>any</em> of the relations you're looking for, in spite of the inclusion of <code>and</code>.</p>

<p>As of the next <a href="https://github.com/craftcms/cms/blob/develop/CHANGELOG-v3.md#309---2018-05-22" rel="nofollow noreferrer">post-3.0.9</a> release, you'll be able to get relational matches from individual Matrix Block instances using a method like so:</p>

<pre><code>{% 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' },
    ])
    .select(['ownerId'])
    .distinct()
    .column()
%}


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

{# display the results #}
Total: {{ entries|length }}&lt;br&gt;

&lt;ul&gt;
    {% for entry in entries %}
        &lt;li&gt;{{ entry.title }}&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p><strong>Original Answer</strong>
Assuming the setup in your question, something like this should work:</p>

<pre><code>{# 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 }}&lt;br&gt;

&lt;ul&gt;
    {% for entry in entries %}
        &lt;li&gt;{{ entry.title }}&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>Here's the relevant "relations" part of the docs: <a href="https://docs.craftcms.com/v3/relations.html" rel="nofollow noreferrer">https://docs.craftcms.com/v3/relations.html</a></p>

CC BY-SA 4.0
2018-05-18T23:49:33.507Z
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
CC BY-SA 4.0
2018-05-18T23:55:25.823Z
Also to let you know some of my records have apostrophes in them, if that makes a difference.
Vin
CC BY-SA 4.0
2018-05-19T01:20:14.870Z
Must be something else affecting it then. If you want to send a database dump over to support@craftcms.com we can test.
Brad Bell
CC BY-SA 4.0
2018-05-19T01:22:47.533Z
I will do, will be good to have this working eventually.  I’ve been working at this for close to 2 weeks.
Vin
CC BY-SA 4.0
2018-05-18T23:27:45.953Z
2018-05-22T22:33:31.347Z
2018-05-22T22:33:31.347Z
Brad Bell
<p>I hope someone has an example or something similar they have done in Craft before.  Her is what I have.</p>

<p><strong>Structure:</strong></p>

<p>Section -> <strong>Flight Company</strong></p>

<p>Matrix Name -> <strong>Plane Details</strong></p>

<p>Matrix Block: <strong>Plane Model and Part</strong></p>

<p>Matrix Fields -> <strong>Model, Part</strong> (field type: Category)</p>

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

<p><strong>Example match needed:</strong></p>

<p>Model = Cessna</p>

<p>Part = Propx45</p>

<p>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.</p>

<p>How best to accomplish this?</p>

CC BY-SA 4.0
2018-05-18T21:33:38.227Z
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
CC BY-SA 4.0
2018-05-18T22:49:59.250Z
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
<matrix><craft3><forloop>
Match Matrix Fields Before Looping Through Results
# Match Matrix Fields Before Looping Through Results

(Asked by: username_0 on 2018-05-16)

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

**Structure:**

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?

**Tags:** matrix, craft3, forloop

> **Comments:**
> **username_1**: 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?  
> **username_0**: 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.  

---

## 1 answers

### Answer by username_1 on 2018-05-18. Score: 2 | Accepted answer

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' },
    ])
    .select(['ownerId'])
    .distinct()
    .column()
%}


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

{# display the results #}
Total: {{ entries|length }}


    {% for entry in entries %}
        {{ entry.title }}
    {% 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 }}


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


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

> **Comments:**
> **username_0**: 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.  
> **username_0**: Also to let you know some of my records have apostrophes in them, if that makes a difference.  
> **username_1**: Must be something else affecting it then. If you want to send a database dump over to support@craftcms.com we can test.  
> **username_0**: I will do, will be good to have this working eventually.  I’ve been working at this for close to 2 weeks.  

---

