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

Taxonomy pages do not allow custom collections

Solved by pamtbaau View solution

Started by swimator 7 years ago · 2 replies · 547 views
7 years ago

I am trying to pull together a custom collection of all item pages (articles in a blog):

{% set my_pages = page.collection({
'items': '@root.descendants',
'order': {
'by': 'random',
'dir': 'desc',
},
})
.ofType('item')
%}

I would assume that no matter on what page I execute this and then run
{{ dump(count(my_pages)) }} - I always get the same count (in our example, let's say I have 50 item pages which have different categories). However, the behavior is a bit different.

  • on the blog page it shows the correct count of pages (50)
  • on an inidivudal item page, it also shows correct count of pages (50)
  • however, if I run this code on a taxonomy listing page: domain.com/blog/category:blue - the count only shows the number of items in category blue (ex: 5)

Do I misunderstand how my custom collection works or do you know why it will not show count 50 on the category page?

thanks much

7 years ago Solution

@swimator, By default, Grav applies the url.params to collections. To alter this behaviour you can use option url_taxonomy_filters: boolean in your collection definition, or change the default in system.yaml using pages:url_taxonomy_filters: boolean.

Try the following:

TWIG
{% set my_pages = page.collection({
    'items': '@root.descendants',
    'order': {
        'by': 'random',
        'dir': 'desc',
    },
    'url_taxonomy_filters': false,
})
.ofType('item')
%}

NB. I think the following would be a more suitable title for your post: "Prevent custom collection being filtered by query params"

last edited 12/14/19 by pamtbaau
7 years ago

@pamtbaau - wow, thanks a lot. It works like a charm.

Perhaps this is something that should be part of the documentation for Page Collections since it is a little counter-intuitive to put some hidden filter onto a custom made collection.

Btw, you are right about the title, but I cannot seem to change it anymore.

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
3 90 50 minutes ago
Support · by Anna, 3 days ago
2 92 22 hours ago
Support · by Justin Young, 23 hours ago
1 40 22 hours ago
Support · by Duc , 1 week ago
2 98 6 days ago
Support · by Colin Hume, 1 week ago
2 90 6 days ago