1

I am trying to query all entry elements related to a category but I am only able to get some of the entries to show up when I run craft.entry().relatedTo(category)

I have a matrix field in the entries of my 'pages' and 'news' section, which you can add categories to a category field to. When I run the above code it only returns entries in the 'news' section that are related to the category. Any help on how I could return all entries in those two sections related to a specific category?

2

If the category field is inside a matrix field, the relation to the entry is sort of indirect (since matrix blocks are elements of their own), so you need to tell Craft to look for the related category there. See Relations -> Going through Matrix.

{% set related = craft.entries().relatedTo({
    targetElement: category,
    field: 'yourMatrixField.yourCategoryField',
}) %}

Replace yourMatrixField with the name of the matrix field containing the category field, and yourCategoryField with the name of the category field containing the relation.

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.