<p>I figured it out.</p>
<p>I changed</p>
<pre><code>{% set category = entry.slug %}
{% set entries = craft.entries.section(section).limit(4).orderby('postDate desc').all() %}
</code></pre>
<p>to</p>
<pre><code>{% set category = craft.categories.slug(entry.slug) %}
{% set entries = craft.entries.section(section).relatedTo(category).limit(4).orderby('postDate desc').all() %}
</code></pre>
<p>and it shows up as expected.</p>

CC BY-SA 4.0
2020-07-04T09:29:03.297Z
did rubber duck helped you? :)
Piotr Pogorzelski
CC BY-SA 4.0
2020-07-04T08:38:21.137Z
2020-07-04T08:38:21.137Z
MarkD
<p>I have set up 3 channel types - A, B &amp; C and a Category. Each entry type shares the same pool of categories.</p>
<p>On the home page, I have created a Matrix field (homeSections) with a block type heroSections that has A, B &amp; C in a dropdown (mediaType) and a Category select (homeSection), so I can have &quot;select mediaType A that has Category 1&quot; as an option.</p>
<p>This is what I have so far</p>
<pre><code>{% set entry = craft.entries().section('home').one() %}

{% for block in entry.homeSections.all() %}

    {% switch block.type %}

    {% case &quot;heroSections&quot; %}

        {% for entry in block.homeSection.all() %}

            &lt;h3&gt;{{ entry.title }} {{ block.mediaType.label }}&lt;/h3&gt;
            
            {% set section = block.mediaType %}
            {% set category = entry.slug %}
            {% set entries = craft.entries.section(section).limit(4).orderby('postDate desc').all() %}

            {% include '_includes/listing' %}
        {% endfor %}

{% endswitch %}

{% endfor %}
</code></pre>
<p>but I can't figure out how to then filter the results so that the category is applied to the entries returned.</p>

<entries><matrix><category><filtering>
Using a Matrix field to create a homepage that shows entries from sections and categories
# Using a Matrix field to create a homepage that shows entries from sections and categories

(Asked by: username_0 on 2020-07-04)

I have set up 3 channel types - A, B & C and a Category. Each entry type shares the same pool of categories.
On the home page, I have created a Matrix field (homeSections) with a block type heroSections that has A, B & C in a dropdown (mediaType) and a Category select (homeSection), so I can have "select mediaType A that has Category 1" as an option.
This is what I have so far
{% set entry = craft.entries().section('home').one() %}

{% for block in entry.homeSections.all() %}

    {% switch block.type %}

    {% case "heroSections" %}

        {% for entry in block.homeSection.all() %}

            {{ entry.title }} {{ block.mediaType.label }}
            
            {% set section = block.mediaType %}
            {% set category = entry.slug %}
            {% set entries = craft.entries.section(section).limit(4).orderby('postDate desc').all() %}

            {% include '_includes/listing' %}
        {% endfor %}

{% endswitch %}

{% endfor %}

but I can't figure out how to then filter the results so that the category is applied to the entries returned.

**Tags:** entries, matrix, category, filtering


---

## 1 answers

### Answer by username_0 on 2020-07-04. Score: 1 | Accepted answer

I figured it out.
I changed
{% set category = entry.slug %}
{% set entries = craft.entries.section(section).limit(4).orderby('postDate desc').all() %}

to
{% set category = craft.categories.slug(entry.slug) %}
{% set entries = craft.entries.section(section).relatedTo(category).limit(4).orderby('postDate desc').all() %}

and it shows up as expected.

> **Comments:**
> **username_1**: did rubber duck helped you? :)  

---

