Collection with root.descendants + filter on content type?

Hi,

I have some content types which are potentially everywhere in the site. I would like to gather them on my home page.

First I did:

        {% set recentnews =
            page.collection({
                'items': '@root.descendants'
            })
            .order('date', 'desc')
            .slice(0,10)
        %}

But I would like to include only some content types

Using somewhere else the events module, I saw:

        {% set events =
            page.collection({
                'items':{
                    '@taxonomy.type':'event',
                },
                'dateRange': {
                    'start': datetools.today,
                    'end': datetools.endOfYear
                },
                'order': {
                    'by': 'date',
                    'dir': 'asc'
                },
                'limit': 5
            })
        %}

So how do I mix the @root.descendants' with the { '@taxonomy.type':'event', }, like syntax ?

Do we also agree that each content type registed as a blueprint of a page has a taxonomy.type equal to blueprints name (or title of blueprint) ? As I tried to use the same syntax as for event above but I didn"t get any result :frowning:

The query I exepect at the end is:

  • From the root of the site, get all pages
  • Filter by content type = “blogpost”
  • Order by date desc
  • Limit 10

Thanks,
Nicolas

Using dump(), I could see that taxonomy.type does not always exist. So should I filter on template ? Is it possible ?

Hmm, I don’t see the point of defining the ‘recentnews’ collection. The ‘events’ will already grab all the page that has for taxonomy 'events’
You can do your filter with just

{% set blogpost =
            page.collection({
                'items':{
                   '@taxonomy.type':'blogpost',
                },
                'dateRange': {
                    'start': datetools.today,
                    'end': datetools.parseDate('next three months')
                },
                'order': {
                    'by': 'date',
                    'dir': 'desc'
                },
                'limit': 10
            })
        %}
``` 
Note that you can use anything for the date range, so 'next three months' should work, and is more 'future-proof.

Hope it helps,

Ok sorry, I should have been more explicit. On the home page, I have 3 blocks in which I want to display :

  • next events ; this is done with the code above and it works, filtering on events type.
  • recents news : where I could display latest pagecvsq (name of the blueprint I created)
  • latest classified ads : where I could display latest classified ads wherever they come from on the site base on the content type “classifiedads” (name of the blueprint)

When I do this, I have no output and from debug previously, I didn’t see the taxonomy.type attribute attached to this content type. At the best, I see the template attribute but don’t know if we can filter on it.

    {% set recentnews = page.collection({
                'items':{
                   '@taxonomy.type':'pagecvsq',
                },
                'order': {
                    'by': 'date',
                    'dir': 'desc'
                },
                'limit': 10
            })
    %}

I saw on the doc or on the forum that I could filter on a tag but I don’t expect user to set them manually as they will forget. I’m ok with this if I can define somewhere that all content for this content type has by default a given tag.

Thanks for your hint and hope it’s clearer now.

I think you can configure your blueprint template to add a taxonomy. See this link: https://learn.getgrav.org/forms/blueprints/advanced-features#replacing-fields-properties-replace

Thanks, I’ll explore this later today and let you know.

For events plugin, it’s define within plugin init: https://github.com/kalebheitzman/grav-plugin-events/blob/master/events.php#L149:L152

		// Add these to taxonomy for events management
		$event_taxonomies = array('type', 'event_freq', 'event_repeat', 'event_location');
		$taxonomy_config = array_merge((array)$this->config->get('site.taxonomies'), $event_taxonomies);
		$this->config->set('site.taxonomies', $taxonomy_config);

I ended up with adding to my blueprint file:

            header.taxonomy.type:
              type: text
              label: type - do not change the value
              default: blogpost

And thus, I can filter as I except. I would have preferred to have this as an hidden value but can’t find the way so far and I don’t expect to make a plugin for my content type to get same behaviour as events plugin.