1

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

enter image description here

This is how it looks when creating a entry: enter image description here

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 %}
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:

  1. Only show rows where showInRandom is checked
  2. Randomize the order of the rows that are returned
  3. 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 }}<br>
{% endfor %}
| improve this answer | |
  • 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 Sep 21 '19 at 23:44
  • @MrGlasspoole It would be easier if you updated your question specifically stating exactly what you are trying to do. – kmgdev Sep 23 '19 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.