Page collection does not filter category

Hi

I am struggling with page collections and filtering. I hope someone can point me in the right direction.

In my default.md frontmatter header I have defined two collections:

---
tutorials:
    items:
        - '@self.children'
        - '@taxonomy':
          category: [excel]
    order:
        by: date
        dir: desc
    limit: 9
    pagination: false
blog:
    items:
        - '@self.children'
        - '@taxonomy':
          category: [blog]
    order:
        by: date
        dir: desc
    limit: 9
    pagination: false
---

In my default.html.twig template I called them using:

{% set tutorials = page.collection('tutorials') %}
{% set blog = page.collection('blog') %}

...

{% for p in tutorials %} code
{% endfor%}

{% for p in blog %}
{% endfor%}

Blog posts from root directory only are showing up, as well as all info related! @self.children and twig commands are working.
The only problem category is not filtered and I have two identical sections showing all posts.

Does anyone have an idea why?

Thank you
Jack

@Jack Are all tutorial and blog pages, children of the current page in the folder structure?

Or in other words, are the collections of pages containing category: [excel] or category: [blog] a subset of the collection @self.children?

In that case, if you remove the ‘@self.children’ you will see proper filtering on category in action.

Hi @pamtbaau

thank you for your reply and suggestion.

Yes all my articles are inside the 01.home folder, each with different categories and tags.

As you suggested, I managed to make it work by removing the @self.children, but I had also to modify my frontmatter from

- '@taxonomy':
  category: [excel]

to

- '@taxonomy.category': excel

As a side note for anyone else.

To have multiple categories in the filter NOT excluding each other, I used

- '@taxonomy.category': excel
- '@taxonomy.category': coding

If you would write the above as

- '@taxonomy.category': [excel, coding]

it would filter articles which have category excel AND category coding.
Which in my case return 0 results.

Is there a better way to write the OR parameter in the frontmatter?

@Jack

Rewriting should not be necessary, but I can reproduce the issue when using your excact format of frontmatter. The size of the collections were 0 when removing - '@self.children'

The reason is a Yaml indentation issue, namely the indentation of category: [excel] and category: [blog]. After correcting the indentation, the results were as expected.

items:
    - '@taxonomy':
        category: [excel]

I have not yet encountered an alternative way…

1 Like