findTaxonomy returning unfiltered results

We are trying to setup a content directory within our site, that can be used to populate subdirectories on the same install. Based on docs, Taxonomy is the way to do it.

So for example, I have a twig template that takes a header variable and uses it in a custom search for taxonomy:

{% set source = page.header.source %}

{% for post in taxonomy.findTaxonomy({
    'category': 'blue',
    'name': source,
    'page': 'purple'
}) %}
  {{ post.content }}
{% endfor %}

Assuming that between the three taxonomies (which are all defined in site.yaml) there is only one content page matching (I’ve confirmed this by using these dummy category and page values on only one page, the documentation leads me to believe that the returned array will have that one page.

However, in my testing, {{ post.content }} spits out the content for all pages that match one of the categories, which means that I actually have to do filtering in the loop? This was my solution, but it requires values to be unique else the variables are just overwritten with the last matching value:

{%  set details = [] %}
{% set feature = [] %}
{% set source = page.header.source %}
{% for subpage in taxonomy.findTaxonomy({
    'category': 'details',
    'name': source,
    'page': 'feature'
}) %}
    {% if "details" in subpage.taxonomy.category %}
        {% set details = subpage %}
    {% endif %}

    {% if "marketing" in subpage.taxonomy.category %}
        {% set feature = subpage %}
    {% endif %}

{% endfor %}

Am I misunderstanding findTaxonomy or is this a bug with the functionality, or something else?

Thanks in advance for help!

I’m pretty sure the findTaxonomy() method does an OR so that it loops through all the items passed in and basically finds anything that matches any of the taxonomies.

However, I can see an AND operation being useful and that would do exactly what you are asking for without the need to filter again.

I will take a look tomorrow at adding an optional param that lets you set the type of search (OR, AND) that you are asking findTaxonomy() to perform. I’ll let you know!


BTW the relevant function is here so you can see how it’s working currently:

Thanks for the quick response! I think I was confused because the docs don’t make that clear. I look forward to seeing what you’re able to come up with!

Documentation appears to be in error :slight_smile: Will fix that also.

Ok after reviewing this I have come to the conclusion that the expected behavior should be an AND relationship, rather than the OR relationship that the code was producing.

I’ve committed a fix to the codebase that defaults to AND between all taxonomies passed into findTaxonomy, but you can also pass in an operator (AND, OR) if you wish to change the behavior.

At this point it’s either or, you can’t have a combination of AND and OR, but I think this is what the expected behavior would be and it’s more flexible than it was. Thanks for bringing this up.

If you want to get the fix for this in your site now (next release will have the fix) you can see this commit:

This is awesome! Thanks so much for quickly reviewing and releasing. We’re rebuilding the whole of with Grav and this is going to make things a lot smoother.

Very cool! I hope you will release a plugin to integrate Grav with FoxyCart?