Skip to content

Instantly share code, notes, and snippets.

@mmikkel
Created March 3, 2015 00:05
Show Gist options
  • Save mmikkel/c6a89a975c0e48f9d560 to your computer and use it in GitHub Desktop.
Save mmikkel/c6a89a975c0e48f9d560 to your computer and use it in GitHub Desktop.
Find entries w/ category relation in Matrix block (Craft CMS)
{# Fetch the category "myCategory" (and any child categories as well) #}
{% set categories = craft.categories.slug( 'myCategory' ) %}
{# Search for entries in section "test", that has the category "myCategory" (or any of its child categories) selected in the field "category" of the Matrix block "testMatrix" #}
{% set entries = craft.entries.section( 'test' ).relatedTo({
targetElement: categories,
field: 'testMatrix.category'
}) %}
{# Output the entries #}
{% for entry in entries %}
{{ entry.title }}<br/>
{% endfor %}
{# Output the categories we filtered against, too #}
{% for category in categories %}
{{ category.title }}
{% endfor %}
@simonicom
Copy link

simonicom commented Apr 7, 2022

This works perfectly !!! Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment