<p>The question is pretty confusing so this answer assumes the following:</p>

<p>You have a Super Table field named <code>entryType</code> with the following cells:</p>

<ul>
<li><code>featuredIn</code> (checkbox)</li>
<li><code>showInRandom</code> (checkbox)</li>
</ul>

<p>And that you are trying to:</p>

<ol>
<li>Only show rows where <code>showInRandom</code> is checked</li>
<li>Randomize the order of the rows that are returned</li>
<li>Limit the returned results to 2</li>
</ol>

<p>Unfortunately Super Table doesn't support searching based on a block's value, <a href="https://github.com/verbb/super-table/issues/290" rel="nofollow noreferrer">which I see you already know</a>, so you'll have to do it in steps.</p>

<p>First, you need to pull in every Super Table row. Then you need to check if each row has <code>showInRandom</code> checked (side note: a lightswich field would be easier), and add its ID to an array if so. Then you randomize the array and run a Super Table query with those IDs and limit the results to 2.</p>

<pre><code>{# create an array to store the returned IDs #}
{% set randomRows = [] %}

{# add only the rows with showInRandom selected to the array #}
{% for row in entry.entryType.all %}
    {% if row.showInRandom.options[0].selected %}
        {% set randomRows = randomRows|merge([row.id]) %}
    {% endif %}
{% endfor %}

{# randomize the array and run a super table query, limiting to 2 #}
{% for row in craft.superTable.blocks({
    owner: entry,
    fieldId: 123,
    id: shuffle(randomRows),
    fixedOrder: true,
    limit: 2
}).all %}
    {{ row.someField }}&lt;br&gt;
{% endfor %}
</code></pre>

CC BY-SA 4.0
2019-09-21T23:44:55.280Z
Sorry that does not work. featuredIn is not a checkbox, it's a field. I did update the question with some screenshots. Seems like i don't get that checkbox thing. Because i also have a field "requiresLogin" and also can't figure out how to make that work.
MrGlasspoole
CC BY-SA 4.0
2019-09-23T19:22:43.317Z
@MrGlasspoole It would be easier if you updated your question specifically stating *exactly* what you are trying to do.
kmgdev
CC BY-SA 4.0
2019-09-18T23:38:32.537Z
2019-09-18T23:38:32.537Z
kmgdev
<p>I have a Super Table field (entryType) with a checkbox field "featuredIn" and some values.  </p>

<p><a href="https://i.sstatic.net/y8Gh8.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/y8Gh8.jpg" alt="enter image description here"></a></p>

<p>This is how it looks when creating a entry:
<a href="https://i.sstatic.net/TQnbO.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/TQnbO.jpg" alt="enter image description here"></a></p>

<p>I can't figure out how to mix "slice" and "orderBy RAND" with filtering by that field.  </p>

<p>This does show me 2 random entries from all:</p>

<pre><code>{% set entries = craft.entries.section('blog').orderBy('RAND()').all() %}

{% for entry in entries|slice(0, 2) %}
  {{ entry.title }}
{% endfor %}
</code></pre>

<p>This shows me only the entries where "showInRandom" is checked (but not random and not only 2):  </p>

<pre><code>{% for entry in craft.entries.section('blog') if entry.entryType.featuredIn.contains('showInRandom') %}
  {{ entry.title }}
{% endfor %}
</code></pre>

<craft3><plugin-supertable>
Show 2 random entries based on checkbox in Super Table
# Show 2 random entries based on checkbox in Super Table

(Asked by: username_0 on 2019-09-15)

I have a Super Table field (entryType) with a checkbox field "featuredIn" and some values.  

This is how it looks when creating a entry:

I can't figure out how to mix "slice" and "orderBy RAND" with filtering by that field.  
This does show me 2 random entries from all:
{% set entries = craft.entries.section('blog').orderBy('RAND()').all() %}

{% for entry in entries|slice(0, 2) %}
  {{ entry.title }}
{% endfor %}

This shows me only the entries where "showInRandom" is checked (but not random and not only 2):  
{% for entry in craft.entries.section('blog') if entry.entryType.featuredIn.contains('showInRandom') %}
  {{ entry.title }}
{% endfor %}

**Tags:** craft3, plugin-supertable

---

## 1 answers

### Answer by username_1 on 2019-09-18. Score: 1

The question is pretty confusing so this answer assumes the following:
You have a Super Table field named entryType with the following cells:

featuredIn (checkbox)
showInRandom (checkbox)

And that you are trying to:

Only show rows where showInRandom is checked
Randomize the order of the rows that are returned
Limit the returned results to 2

Unfortunately Super Table doesn't support searching based on a block's value, which I see you already know, so you'll have to do it in steps.
First, you need to pull in every Super Table row. Then you need to check if each row has showInRandom checked (side note: a lightswich field would be easier), and add its ID to an array if so. Then you randomize the array and run a Super Table query with those IDs and limit the results to 2.
{# create an array to store the returned IDs #}
{% set randomRows = [] %}

{# add only the rows with showInRandom selected to the array #}
{% for row in entry.entryType.all %}
    {% if row.showInRandom.options[0].selected %}
        {% set randomRows = randomRows|merge([row.id]) %}
    {% endif %}
{% endfor %}

{# randomize the array and run a super table query, limiting to 2 #}
{% for row in craft.superTable.blocks({
    owner: entry,
    fieldId: 123,
    id: shuffle(randomRows),
    fixedOrder: true,
    limit: 2
}).all %}
    {{ row.someField }}
{% endfor %}

> **Comments:**
> **username_0**: Sorry that does not work. featuredIn is not a checkbox, it's a field. I did update the question with some screenshots. Seems like i don't get that checkbox thing. Because i also have a field "requiresLogin" and also can't figure out how to make that work.  
> **username_1**: @MrGlasspoole It would be easier if you updated your question specifically stating *exactly* what you are trying to do.  

---

