Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Page collection does not filter category

Solved by pamtbaau View solution

Started by Giaco 8 years ago · 3 replies · 940 views
8 years ago

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:

YAML
---
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:

TWIG
{% 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

8 years ago Solution

@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.

8 years ago

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

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

to

TXT
- '@taxonomy.category': excel

As a side note for anyone else.

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

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

If you would write the above as

TXT
- '@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?

8 years ago

@Jack

@Jack:
but I had also to modify my frontmatter

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.

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

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

I have not yet encountered an alternative way...

👍 1

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 60 16 hours ago
Support · by Anna, 3 days ago
2 66 18 hours ago
Support · by Justin Young, 19 hours ago
1 33 19 hours ago
Support · by Duc , 1 week ago
2 69 6 days ago
Support · by Colin Hume, 1 week ago
2 61 6 days ago