Collection with multiple item constraints

I have a folder /details of which some page have a category.

A collection defined as follows…

items:
  - '@page.children': /details
  - '@taxonomy.category': [MyCategory]

…will contain all child pages of folder /details plus all pages with category ‘MyCategory’. Since the both items are in folder /details I get all pages in /details

How do I create a collection that only contains pages from folder /details AND also have category ‘MyCategory’;

@Vince42,

I see two alternatives depending on your situation:

  1. If there are also pages with category ‘MyCategory’ outside folder /details, you will need to create two separate collections and apply method intersect.

    See Multiple Collections to create two collections in a page. One for all pages in the /details folder and the other for all pages with category “MyCategory”.

    Then in Twig, get both collections from the page and use an “intersect” (see Collection Object Methods):

    Collection::intersect($collection2) Merge two collections, keeping items that occur in both collections (like an “AND” condition)

  2. If there are no categories outside folder /details, then the following will give only the pages with the proper category:

    items:
      - '@taxonomy.category': [MyCategory]
    

Perfect, it worked out of the box!
Frontmatter of the module:

pages:
    items: '@page.children': /details
taxonomies:
    items: '@taxonomy.category': [Service]

and in pages.html.wtig:

{% set pages = page.collection('pages') %}
{% set taxonomies = page.collection('taxonomies') %}
{% set collection = pages.intersect(taxonomies) %}

:slight_smile: