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

Chaining Page Collections

Started by Muut Archive 10 years ago · 4 replies · 399 views
10 years ago

Just wondering if there is any way to chain page collections.

Some context:

I want to find all of the child pages of 'Blog A' that have the category 'Tech'. Is that possible?

Currently, if i do:

YAML
content:
  items:
    '@taxonomy':
      category: [Tech]

I will get all pages with category 'Tech'.

If I do:

YAML
content:
  items:
    - '@page' = /blog-a
    - '@taxonomy': 
         category: [Tech]

I will get all the pages in Blog A, and all pages with category 'Tech' (even in they are under /blog-b)

10 years ago

Basically everything that Collections can currently handle is detailed in these docs

The multiple collections return the sum of the collections. The best thing to do is to get all the items then filter out the ones you don't want as you iterate over them.

10 years ago

For anyone trying to do this, I had to do it like so:

PHP

private function getItems($page) {
   $page->collection($params, $pagination)->filter(array($this, 'filter');
}

public function filter($value, $key) {
   // If key is not in target page collection, filter it from results.
   $children = $this->target->children();
   if ($children->offsetGet($key)) {
      return true;
   } else {
      return false;
   }
}
---
10 years ago

Was it not achievable without custom PHP functions? Also, surely it would be more efficient to allow for the addition of partial collections to a larger one, rather than filtering out content from multiple large ones.

10 years ago

From what I can see, there is no way with page collections to get pages tagged under a specific category, limited by children of a page.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1357 9 years ago
Archive · by Muut Archive, 9 years ago
2 936 9 years ago
Archive · by Muut Archive, 9 years ago
2 4066 9 years ago
Archive · by Muut Archive, 9 years ago
1 2954 9 years ago
Archive · by Muut Archive, 9 years ago
3 1120 9 years ago