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

Multiple taxonomy collection with pagination, limit, and order in twig?

Started by Christopher Pickert 9 years ago · 2 replies · 1647 views
9 years ago

In a twig template, how can I request a collection with pagination, limit, and order using “findTaxonomy”? I need to create a list of the latest posts in certain categories. The following experiment does not work:

TWIG
{% set blog_collection = taxonomy.findTaxonomy({'category':['Music', 'Film']}, 'or').visible().published().setParams({'limit': 5, 'pagination': true}).order('date', 'desc') %}

Alternately, is there some other way to accomplish the same thing? I did try the usual way but it seems to only allow one category. That is, I can't say {'@taxonomy.category': 'Film, Music'} or {'@taxonomy.category': ['Film', 'Music']} in the following:

TWIG
{% set blog_options = { items: {'@taxonomy.category': 'Film'}, 'limit': 3, 'order': {'by': 'date', 'dir': 'desc'}, 'pagination': false } %}
{% set blog_collection = page.collection(blog_options) %}

Note that I cannot use frontmatter in this case, as there are other parts of this complex page with different needs. Plus I really want to know how this works in twig.

I tried posting this question but somehow it got archived so I'm trying again.

9 years ago

I think findTaxonomy will fetch only the published page, so you can remove .published from the filter. Also, it seems like you are checking for .visible which means it will only displays page that are displayed in the menu.

You could try adding the parameters and filter one by one and see if it works.

Hope it helps

7 years ago

In order to make taxonomy collections filters like .published() .nonPublished() etc., for example I needed to list the future published articles for logged in admins.

\system\src\Grav\Common\Taxonomy.php, around line 56

PHP

    public function addTaxonomy(PageInterface $page, $page_taxonomy = null)
    {
        if (!$page_taxonomy) {
            $page_taxonomy = $page->taxonomy();
        }

/*        
        if (empty($page_taxonomy) || !$page->published()) {
            return;
        } 
*/

        if (empty($page_taxonomy)) {
            return;
        }

        /** @var Config $config */
        $config = $this->grav['config'];
        if ($config->get('site.taxonomies')) {
            foreach ((array)$config->get('site.taxonomies') as $taxonomy) {
                if (isset($page_taxonomy[$taxonomy])) {
                    foreach ((array)$page_taxonomy[$taxonomy] as $item) {
                        $this->taxonomy_map[$taxonomy][(string)$item][$page->path()] = ['slug' => $page->slug()];
                    }
                }
            }
        }
    }
last edited 05/25/19 by Mihai Ciprian Chezan

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 53 10 hours ago
Support · by Anna, 3 days ago
2 60 13 hours ago
Support · by Justin Young, 14 hours ago
1 30 14 hours ago
Support · by Duc , 1 week ago
2 65 5 days ago
Support · by Colin Hume, 1 week ago
2 56 5 days ago