Nested Matrix functionality for one block type using Tags I have an entry with two matrix fields and I want to use relations between them using Tags to create a fake nested Matrix functionality for one block type. The "main" matrix field is for creating a typical banded page (several horisontal stripes of content). One of the blocks in this main matrix has to contain a contentSlider, and since in this specific case adding an Assets field is not sufficient we have to nest or merge two matrix fields.

While looping through the main matrix field using the Switch method I have a block type named "contentSlider". In this block type I have a Tags field named "showTags". In my second matrix field named "slidesMatrix" I have a field named "tags". I want to find blocks in the second matrix (slidesMatrix) related to the current block in the main matrix.



Here is my attempt, it is not giving me any errors but it is not outputting the "slide.heading" field either so something must be wrong:

{% case "contentSlider" %}
    {% set tagsToFind = block.showTags %}
    {% set relatedSlides = entry.slidesMatrix.tags.relatedTo(tagsToFind).order('score').find() %}
    {% for slide in relatedSlides %}
        Matched Tag in: {{ slide.heading }}&lt;br&gt;
    {% endfor %}




This alternative code works, but it is only matching the first Tag from the current block to all the Tags in the second matrix block (slidesMatrix.tags) and switching to one of the native Craft methods like "relatedTo" would be a more powerful solution.

{% case "contentSlider" %}
    {% set tagsToFind = block.showTags %}
    {% set tagToFind = tagsToFind.first.title %}
    {% for slide in entry.slidesMatrix if tagToFind in slide.tags %}
        Matched Tag in: {{ slide.heading }}&lt;br&gt;
    {% endfor %}            




To sum this up into one question;
How can I loop through a matrix field filtered by Tags contained in a tags fields inside the matrix blocks I want to loop through, using a native Craft feature like 'relatedTo'?
I just tried to replicate your setup (in simplified form), because I thought this is in fact an interesting and creative approach
to do Matrix nesting that might come handy.

Here's my code that works great for me. I hope it helps, even though I actually don't see the difference to what you have?!

{# Go through our Main Matrix blocks #}
{% for block in entry.mainMatrix.type('mainRelationsBlock') %}

    {# Get the tags selected in the Main Matrix block #}
    {% set tagsToFind = block.mainTags %}

    {# Try filtering Matrix blocks of our Sub Matrix using `relatedTo` #}
    {% set relatedBlocks = entry.subMatrix.relatedTo({
        targetElement: tagsToFind
    }) %}

    {# Show them #}
    {% for relatedBlock in relatedBlocks %}
        &lt;li&gt;{{ relatedBlock.text }}&lt;/li&gt;
    {% endfor %}

{% endfor %}