<p>You can do it, but you’ll need to break it into a few steps:</p>
<ol>
<li>Get the Matrix blocks having the desired checked size values.</li>
<li>Get the IDs of the <em>Things</em> entries that own those Matrix blocks.</li>
<li>Get the <em>Colors</em> entries related to those <em>Things</em> entries by their IDs.</li>
</ol>
<p>For context, we can quickly dump out our <em>Things</em> and their sizes along with our <em>Colors</em> and their related <em>Things</em>:</p>
<pre><code>{# ------------------------------------------------------ #}
{# Establishing Context                                   #}
{# ------------------------------------------------------ #}

{% set things = craft.entries().section('things').all() %}
{% set colors = craft.entries().section('colors').all() %}

&lt;h2&gt;Things&lt;/h2&gt;
&lt;ul&gt;
  {% for thing in things %}
    &lt;li&gt;
      {{ thing.title }}&lt;br&gt;
      {% for size in thing.thingsBlocks.type('sizes').one().sizes %}
        &lt;code&gt;✓ {{ size }}&lt;/code&gt;
        {% if not loop.last %}&lt;br&gt;{% endif %}
      {% endfor %}
    &lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;

&lt;h2&gt;Colors&lt;/h2&gt;
&lt;ul&gt;
  {% for color in colors %}
    &lt;li&gt;
      {{ color.title }}&lt;br&gt;
      {% for thing in color.thingsEntries.all() %}
        &lt;code&gt;{{ thing.title }}&lt;/code&gt;
        {% if not loop.last %}&lt;br&gt;{% endif %}
      {% endfor %}
    &lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;
</code></pre>
<p>And the query might look something like this:</p>
<pre><code>{# ------------------------------------------------------ #}
{# The Part with the Answer                               #}
{# ------------------------------------------------------ #}

&lt;h2&gt;Colors with Specifically-Sized Things&lt;/h2&gt;

{# Specify the checkbox size values we want #}
{% set targetSizes = ['sizeA', 'sizeD'] %}

&lt;p&gt;Colors entries related to Things with {{ targetSizes|join(' or ') }}.&lt;/p&gt;

{# 1+2. Get IDs of entries owning the Matrix blocks with our desired sizes #}
{% set targetThingsEntryIds = craft.matrixBlocks()
  .field('thingsBlocks')
  .type('sizes')
  .sizes(targetSizes)
  .select('ownerId')
  .column() %}

{# 3. Query Colors entries with relationships to the target Things entries #}
{% set colorsWithSizedThings = craft.entries()
  .section('colors')
  .thingsEntries(targetThingsEntryIds)
  .all() %}

{# Tersely output list of resulting entry titles #}
{{ ul(colorsWithSizedThings|map((entry) =&gt; &quot;#{entry.title}&quot;)) }}
</code></pre>
<p><code>colorsWithSizedThings</code> will work with <a href="https://craftcms.com/docs/3.x/dev/tags.html#paginate" rel="nofollow noreferrer">pagination</a> like any other element query.</p>
<p>That <code>.sizes(targetSizes)</code> part will query either of the provided values (<code>or</code>). See the <a href="https://craftcms.com/docs/3.x/checkboxes-fields.html#querying-elements-with-checkboxes-fields" rel="nofollow noreferrer">Checkboxes Fields</a> page for other query options including treating the value array as an <code>and</code>—meaning all provided options must be checked rather than <em>any</em> of them.</p>
<hr />
<p><strong>Edit 11/30/21</strong></p>
<p>Andrew demonstrated how to accomplish this using a custom behavior:<br />
<a href="https://nystudio107.com/blog/searching-craft-cms-matrix-blocks" rel="nofollow noreferrer">Searching Craft CMS Matrix Blocks</a></p>

CC BY-SA 4.0
2021-11-04T04:02:01.107Z
2021-11-30T22:43:13.273Z
2021-11-30T22:43:13.273Z
Matt Stein
<p>Let’s say I have a site with two sections: <em>Things</em> and <em>Colors</em>.</p>
<p><em>Things</em> has a Matrix field (<code>thingsBlocks</code>) with a <em>Sizes</em> block type. That <em>Sizes</em> block type has a checkboxes field (<code>sizes</code>) with values that will be important again in a moment.</p>
<p>The <em>Colors</em> section has an entries field (<code>thingsEntries</code>) for relationships to <em>Things</em>.</p>
<p><strong>My goal</strong> is to query <em>Colors</em> entries related to <em>Things</em> that have <em>Sizes</em> blocks with specific size values checked, and I’d like this to be an element query I can use with pagination.</p>
<p>The following pseudo-code is pure fantasy and does not work:</p>
<pre><code>{% set colorsWithSizedThings = craft.entries()
  .section('colors')
  .thingsEntries(['thingsBlocks' =&gt; ['sizes' =&gt; ['sizeA', 'sizeD']])
  .all()
</code></pre>

<entries><matrix><entries-field><checkboxes>
How can I query entries related to another section’s entries having specific Matrix checkbox field values?
# How can I query entries related to another section’s entries having specific Matrix checkbox field values?

(Asked by: username_0 on 2021-11-04)

Let’s say I have a site with two sections: Things and Colors.
Things has a Matrix field (thingsBlocks) with a Sizes block type. That Sizes block type has a checkboxes field (sizes) with values that will be important again in a moment.
The Colors section has an entries field (thingsEntries) for relationships to Things.
My goal is to query Colors entries related to Things that have Sizes blocks with specific size values checked, and I’d like this to be an element query I can use with pagination.
The following pseudo-code is pure fantasy and does not work:
{% set colorsWithSizedThings = craft.entries()
  .section('colors')
  .thingsEntries(['thingsBlocks' => ['sizes' => ['sizeA', 'sizeD']])
  .all()

**Tags:** entries, matrix, entries-field, checkboxes


---

## 1 answers

### Answer by username_0 on 2021-11-04. Score: 1 | Accepted answer

You can do it, but you’ll need to break it into a few steps:

Get the Matrix blocks having the desired checked size values.
Get the IDs of the Things entries that own those Matrix blocks.
Get the Colors entries related to those Things entries by their IDs.

For context, we can quickly dump out our Things and their sizes along with our Colors and their related Things:
{# ------------------------------------------------------ #}
{# Establishing Context                                   #}
{# ------------------------------------------------------ #}

{% set things = craft.entries().section('things').all() %}
{% set colors = craft.entries().section('colors').all() %}

Things

  {% for thing in things %}
    
      {{ thing.title }}
      {% for size in thing.thingsBlocks.type('sizes').one().sizes %}
        ✓ {{ size }}
        {% if not loop.last %}{% endif %}
      {% endfor %}
    
  {% endfor %}


Colors

  {% for color in colors %}
    
      {{ color.title }}
      {% for thing in color.thingsEntries.all() %}
        {{ thing.title }}
        {% if not loop.last %}{% endif %}
      {% endfor %}
    
  {% endfor %}


And the query might look something like this:
{# ------------------------------------------------------ #}
{# The Part with the Answer                               #}
{# ------------------------------------------------------ #}

Colors with Specifically-Sized Things

{# Specify the checkbox size values we want #}
{% set targetSizes = ['sizeA', 'sizeD'] %}

Colors entries related to Things with {{ targetSizes|join(' or ') }}.

{# 1+2. Get IDs of entries owning the Matrix blocks with our desired sizes #}
{% set targetThingsEntryIds = craft.matrixBlocks()
  .field('thingsBlocks')
  .type('sizes')
  .sizes(targetSizes)
  .select('ownerId')
  .column() %}

{# 3. Query Colors entries with relationships to the target Things entries #}
{% set colorsWithSizedThings = craft.entries()
  .section('colors')
  .thingsEntries(targetThingsEntryIds)
  .all() %}

{# Tersely output list of resulting entry titles #}
{{ ul(colorsWithSizedThings|map((entry) => "#{entry.title}")) }}

colorsWithSizedThings will work with pagination like any other element query.
That .sizes(targetSizes) part will query either of the provided values (or). See the Checkboxes Fields page for other query options including treating the value array as an and—meaning all provided options must be checked rather than any of them.

Edit 11/30/21
Andrew demonstrated how to accomplish this using a custom behavior:
Searching Craft CMS Matrix Blocks


---

