Answers.Body: <p>Instead of going through the entries and manually searching their super table blocks, I would query the super table blocks directly. You can use <a href="https://verbb.io/craft-plugins/super-table/docs/template-guides/block-querying" rel="nofollow noreferrer">Super Table Block Queries</a> to find specific blocks. For example:</p>
  <pre class="lang-twig prettyprint-override"><code>{% set stateRestrictionsField = craft.app.fields.getFieldByHandle('stateRestrictions') %}
  {% set state = 'Nebraska' %}
  {% set restrictionsForState = craft.superTable.blocks()
      .fieldId(stateRestrictionsField.id)
      .region(state)
      .all()
  %}
  {% for block in restrictionsForState %}
      {% set program = block.owner %}
      {# … #}
  {% endfor %}
  </code></pre>
  <p>You may need to adjust the query (for dropdown fields, you can search by value instead of label), but you get the idea. This will find all super table blocks in the <code>stateRestrictions</code> field with the <code>region</code> set to Nebraska. Then you can iterate through those directly. If you need access to the entry the block is on, you can access it with <code>block.owner</code> as in the example above.</p>
  <p>If you really need a list of entries with those blocks (e.g. to sort them), you can map the array of super table blocks to an array of entries like this:</p>
  <pre><code>{% set entriesWithStateRestriction = restrictionsForState|map(block =&gt; block.owner) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-24T07:01:36.513Z
Answers.Text: Aha excellent, that is great. I didn't know about `block.owner`, which is very helpful in this case. I set the state to `option.label`, which also will work through each state in the field dropdown.
Answers.UserDisplayName: Erol
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-22T10:26:31.703Z
Answers.LastActivityDate: 2023-03-22T10:26:31.703Z
Answers.UserDisplayName: MoritzLost
Body: <p>I've been having trouble figuring out how to query my entries for another template. Currently, I have a channel of entries for &quot;programs.&quot; These programs have a super table that contains a list of states (<code>stateRestrictions</code>). You can add an individual state, then input some text description details about that program in that state. For example, under the program for &quot;dog walker,&quot; you can add &quot;Nebraska&quot; as a state with details about the program. Each program can have any number of states.</p>
  <p>On a separate template, I want to query all programs that have a super table entry with &quot;Nebraska&quot; and list them under that state.</p>
  <p>That template looks like this:</p>
  <pre><code>{% extends &quot;layout/main&quot; %}
  
  {% block main %}
  
      &lt;section class=&quot;base global-modules&quot;&gt;
          &lt;div class=&quot;constrained-layout state-restrictions&quot;&gt;
              {% set stateRestriction = craft.app.fields.getFieldByHandle('stateRestrictions') %}
              {% for field in stateRestriction.blockTypeFields if field.handle=='region' %}
                  {% for option in field.options %}
                      &lt;div class=&quot;state&quot;&gt;
                          &lt;h2 class=&quot;title&quot;&gt;{{ option.label }}&lt;/h2&gt;
                          {% for entry in craft.entries.section('programs').all() %}
                              {% if entry.stateRestrictions|length %}
                                  {% for row in entry.stateRestrictions %}
                                      {% if row.region == option.value %}
                                          &lt;div class=&quot;program&quot;&gt;
                                              &lt;h3 class=&quot;title&quot;&gt;&lt;a href=&quot;{{ entry.url }}&quot;&gt;{{ entry.title }}&lt;/a&gt;&lt;/h3&gt;
                                              &lt;div class=&quot;label {{ row.restrictionLevel.value }}&quot;&gt;{{ row.restrictionLevel.label }}&lt;/div&gt;
                                              &lt;div class=&quot;description&quot;&gt;
                                                  {{ row.text }}
                                              &lt;/div&gt;
                                          &lt;/div&gt;
                                      {% endif %}
                                  {% endfor %}
                              {% endif %}
                          {% endfor %}
                          &lt;p class=&quot;note&quot;&gt;This state has no program restrictions.&lt;/p&gt;
                      &lt;/div&gt;
                  {% endfor %}
              {% endfor %}
          &lt;/div&gt;
      &lt;/section&gt;
  
  {% endblock %}
  </code></pre>
  <p>I'm trying to query the stateRestrictions dropdown of states to generate the list of states on this template, then show the programs that have that state as a filled value, but my approach seems convoluted and also unable to handle the case where there are no restrictions and a state should just show &quot;No restrictions.&quot; It is a bit confusing since the query needs to bubble up from within the program entry. Is there a better way of approaching this?</p>
  
Tags: <relatedto><plugin-supertable><entry-query>
Title: Looping through Entries that Match Criteria with Fields inside the Same Entries
ThreadText: # Looping through Entries that Match Criteria with Fields inside the Same Entries
  
  (Asked by: username_0 on 2023-03-22)
  
  I've been having trouble figuring out how to query my entries for another template. Currently, I have a channel of entries for "programs." These programs have a super table that contains a list of states (`stateRestrictions`). You can add an individual state, then input some text description details about that program in that state. For example, under the program for "dog walker," you can add "Nebraska" as a state with details about the program. Each program can have any number of states.
  
  On a separate template, I want to query all programs that have a super table entry with "Nebraska" and list them under that state.
  
  That template looks like this:
  
  ```
  {% extends "layout/main" %}
  
  {% block main %}
  
      <section class="base global-modules">
          <div class="constrained-layout state-restrictions">
              {% set stateRestriction = craft.app.fields.getFieldByHandle('stateRestrictions') %}
              {% for field in stateRestriction.blockTypeFields if field.handle=='region' %}
                  {% for option in field.options %}
                      <div class="state">
                          <h2 class="title">{{ option.label }}</h2>
                          {% for entry in craft.entries.section('programs').all() %}
                              {% if entry.stateRestrictions|length %}
                                  {% for row in entry.stateRestrictions %}
                                      {% if row.region == option.value %}
                                          <div class="program">
                                              <h3 class="title"><a href="{{ entry.url }}">{{ entry.title }}</a></h3>
                                              <div class="label {{ row.restrictionLevel.value }}">{{ row.restrictionLevel.label }}</div>
                                              <div class="description">
                                                  {{ row.text }}
                                              </div>
                                          </div>
                                      {% endif %}
                                  {% endfor %}
                              {% endif %}
                          {% endfor %}
                          <p class="note">This state has no program restrictions.</p>
                      </div>
                  {% endfor %}
              {% endfor %}
          </div>
      </section>
  
  {% endblock %}
  
  ```
  
  I'm trying to query the stateRestrictions dropdown of states to generate the list of states on this template, then show the programs that have that state as a filled value, but my approach seems convoluted and also unable to handle the case where there are no restrictions and a state should just show "No restrictions." It is a bit confusing since the query needs to bubble up from within the program entry. Is there a better way of approaching this?
  
  **Tags:** relatedto, plugin-supertable, entry-query
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-03-22. Score: 1 | Accepted answer
  
  Instead of going through the entries and manually searching their super table blocks, I would query the super table blocks directly. You can use [Super Table Block Queries](https://verbb.io/craft-plugins/super-table/docs/template-guides/block-querying) to find specific blocks. For example:
  
  ```twig
  {% set stateRestrictionsField = craft.app.fields.getFieldByHandle('stateRestrictions') %}
  {% set state = 'Nebraska' %}
  {% set restrictionsForState = craft.superTable.blocks()
      .fieldId(stateRestrictionsField.id)
      .region(state)
      .all()
  %}
  {% for block in restrictionsForState %}
      {% set program = block.owner %}
      {# … #}
  {% endfor %}
  
  ```
  
  You may need to adjust the query (for dropdown fields, you can search by value instead of label), but you get the idea. This will find all super table blocks in the `stateRestrictions` field with the `region` set to Nebraska. Then you can iterate through those directly. If you need access to the entry the block is on, you can access it with `block.owner` as in the example above.
  
  If you really need a list of entries with those blocks (e.g. to sort them), you can map the array of super table blocks to an array of entries like this:
  
  ```
  {% set entriesWithStateRestriction = restrictionsForState|map(block => block.owner) %}
  
  ```
  
  > **Comments:**
  > **username_0**: Aha excellent, that is great. I didn't know about `block.owner`, which is very helpful in this case. I set the state to `option.label`, which also will work through each state in the field dropdown.  
  
  ---
  
  