Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-04T04:02:01.107Z
Answers.LastActivityDate: 2021-11-30T22:43:13.273Z
Answers.LastEditDate: 2021-11-30T22:43:13.273Z
Answers.UserDisplayName: Matt Stein
Body: <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>
  
Tags: <entries><matrix><entries-field><checkboxes>
Title: How can I query entries related to another section’s entries having specific Matrix checkbox field values?
ThreadText: # 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:
  
  1. Get the Matrix blocks having the desired checked size values.
  2. Get the IDs of the *Things* entries that own those Matrix blocks.
  3. 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() %}
  
  <h2>Things</h2>
  <ul>
    {% for thing in things %}
      <li>
        {{ thing.title }}<br>
        {% for size in thing.thingsBlocks.type('sizes').one().sizes %}
          <code>✓ {{ size }}</code>
          {% if not loop.last %}<br>{% endif %}
        {% endfor %}
      </li>
    {% endfor %}
  </ul>
  
  <h2>Colors</h2>
  <ul>
    {% for color in colors %}
      <li>
        {{ color.title }}<br>
        {% for thing in color.thingsEntries.all() %}
          <code>{{ thing.title }}</code>
          {% if not loop.last %}<br>{% endif %}
        {% endfor %}
      </li>
    {% endfor %}
  </ul>
  
  ```
  
  And the query might look something like this:
  
  ```
  {# ------------------------------------------------------ #}
  {# The Part with the Answer                               #}
  {# ------------------------------------------------------ #}
  
  <h2>Colors with Specifically-Sized Things</h2>
  
  {# Specify the checkbox size values we want #}
  {% set targetSizes = ['sizeA', 'sizeD'] %}
  
  <p>Colors entries related to Things with {{ targetSizes|join(' or ') }}.</p>
  
  {# 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](https://craftcms.com/docs/3.x/dev/tags.html#paginate) like any other element query.
  
  That `.sizes(targetSizes)` part will query either of the provided values (`or`). See the [Checkboxes Fields](https://craftcms.com/docs/3.x/checkboxes-fields.html#querying-elements-with-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](https://nystudio107.com/blog/searching-craft-cms-matrix-blocks)
  
  
  ---
  
  