# \[SOLVED\] How to create union list of terms of two taxonomies?

**URL:** <https://discourse.gohugo.io/t/solved-how-to-create-union-list-of-terms-of-two-taxonomies/9539>\
**Category:** support\
**Created:** [December 8, 2017, 11:23am UTC](https://discourse.gohugo.io/t/solved-how-to-create-union-list-of-terms-of-two-taxonomies/9539 "2017-12-08T11:23:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikhail](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mikhail/32/1138_2.png) [@Mikhail](https://discourse.gohugo.io/u/Mikhail)\
**Post date:** [December 8, 2017, 11:23am UTC](https://discourse.gohugo.io/t/solved-how-to-create-union-list-of-terms-of-two-taxonomies/9539/1 "2017-12-08T11:23:53Z")

</div>

In the template `\layout\taxonomy\person.terms.html` I add this strings:

```auto
{{ $authors := $.Site.Taxonomies.authors }}
{{ $recipients := $.Site.Taxonomies.recipients }}
{{ $un1 := union $authors $recipients }}

```

Hogo 0.31.1 generate error `at <union $authors $reci...>: error calling union: can't iterate over hugolib.Taxonomy`

---

<div class="post-metadata">

**Author:** ![Scherb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/scherb/32/4789_2.png) [@Scherb](https://discourse.gohugo.io/u/Scherb)\
**Post date:** [December 9, 2017, 1:22am UTC](https://discourse.gohugo.io/t/solved-how-to-create-union-list-of-terms-of-two-taxonomies/9539/2 "2017-12-09T01:22:00Z")

</div>

I think the taxonomy map can’t be unioned because it is not an array or slice (types).

However, I was able to use scratch and a loop to collect specific terms, test below hope this helps as a solution, if you were merging to pass to something else (template, etc), not sure of how you are using “$un1”.

**Example:**  
I use var shortcut to .Scratch.

```
{{ $var := .Scratch }}

<!-- Collect Specific cateogries -->
{{ range $taxName, $terms := .Site.Taxonomies.authors }}
    {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "term" . ))}}
{{ end }}
{{ range $taxName, $terms := .Site.Taxonomies.recipients }}
    {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "term" . ))}}
{{ end }}

<!-- Test (presumably your template) -->
{{ range ($var.Get "mergedTax") }}

    <!-- Terms key -->
    <p><strong>{{ .termName }}</strong></p>

     <!-- Terms map (weighted pages, if needed) -->
    <p>{{ .term.Pages }}</p>
{{ end }}

```

**Notes:**

- Using .Scratch.Add to push into an array (collection)
- Using “slice” to indicate this is an array item for the “.Add” method
- Using dict to collect the term similar to it’s original map structure (to get the key and the map)
- Looping though the final created array or dictionary items (key/value pairs) for testing that all were collected

Hope this helps…

---

<div class="post-metadata">

**Author:** ![Mikhail](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mikhail/32/1138_2.png) [@Mikhail](https://discourse.gohugo.io/u/Mikhail)\
**Post date:** [December 11, 2017, 10:31am UTC](https://discourse.gohugo.io/t/solved-how-to-create-union-list-of-terms-of-two-taxonomies/9539/3 "2017-12-11T10:31:50Z")

</div>

Thank you, your code works well. But I can not remove duplicate terms.

First I tried:

```auto
{{ range sort (uniq ($var.Get "mergedTax")) "termName" }}

```

The `sort` works, but the `uniq` does not work and does not generate an error.

Then I tried it in the second range:

```auto
{{ range $taxName, $terms := .Site.Taxonomies.recipients }}
      {{ if not (in ($var.Get "mergedTax") $taxName) }}
            {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "term" .))}}
      {{ end }}
{{ end }}

```

The `in` does not work and does not generate an error.

Maybe you have any ideas for removing duplicates?

---

<div class="post-metadata">

**Author:** ![Scherb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/scherb/32/4789_2.png) [@Scherb](https://discourse.gohugo.io/u/Scherb)\
**Post date:** [December 11, 2017, 2:42pm UTC](https://discourse.gohugo.io/t/solved-how-to-create-union-list-of-terms-of-two-taxonomies/9539/4 "2017-12-11T14:42:45Z")

</div>

I think your approach would work if we get the names into an array by themselves. The “mergedTax” array is an array of dictionaries (like objects). Below I wrote out what this would look, I added another array to collect the names to check if they had been collected yet. This code is untested…

```
{{ $var := .Scratch }}

<!-- Collect Specific cateogries -->
{{ range $taxName, $terms := .Site.Taxonomies.authors }}
  {{ $var.Add "mergedTaxNames" (slice $taxName) }}
  {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "term" . ))}}
{{ end }}
{{ range $taxName, $terms := .Site.Taxonomies.recipients }}
    {{ if not (in ($var.Get "mergedTaxNames") $taxName) }}
        {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "term" . ))}}
    {{ end }}
{{ end }}

<!-- Test (presumably your template) -->
{{ range ($var.Get "mergedTax") }}

    <!-- Terms key -->
    <p><strong>{{ .termName }}</strong></p>

    <!-- Terms map (weighted pages, if needed) -->
    <p>{{ .term.Pages }}</p>
{{ end }}
```

---

<div class="post-metadata">

**Author:** ![Mikhail](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mikhail/32/1138_2.png) [@Mikhail](https://discourse.gohugo.io/u/Mikhail)\
**Post date:** [December 11, 2017, 3:42pm UTC](https://discourse.gohugo.io/t/solved-how-to-create-union-list-of-terms-of-two-taxonomies/9539/5 "2017-12-11T15:42:56Z")

</div>

Thanks again. A combined list of terms for the three taxonomies. The final code that generates hyperlinks.

```auto
{{ $var := .Scratch }}

<!-- Collect Specific cateogries -->
{{ range $taxName, $terms := .Site.Taxonomies.authors }}
    {{ $var.Add "mergedTaxNames" (slice $taxName) }}
    {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "path" "authors"))}}
{{ end }}
{{ range $taxName, $terms := .Site.Taxonomies.recipients }}
    {{ if not (in ($var.Get "mergedTaxNames") $taxName) }}
        {{ $var.Add "mergedTaxNames" (slice $taxName) }}
        {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "path" "recipients"))}}
    {{ end }}
{{ end }}
{{ range $taxName, $terms := .Site.Taxonomies.persons }}
    {{ if not (in ($var.Get "mergedTaxNames") $taxName) }}
        {{ $var.Add "mergedTax" (slice (dict "termName" $taxName "path" "persons"))}}
    {{ end }}
{{ end }}

<ul>
{{ range sort ($var.Get "mergedTax") "termName" }}
    <li><p><a href="/{{ .path }}/{{ urlize .termName }}/">{{ .termName }}</a></p></li>
{{ end }}
</ul>

```

---

<div class="post-metadata">

**Author:** ![szac](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/szac/32/5358_2.png) [@szac](https://discourse.gohugo.io/u/szac)\
**Post date:** [February 9, 2018, 11:31pm UTC](https://discourse.gohugo.io/t/solved-how-to-create-union-list-of-terms-of-two-taxonomies/9539/6 "2018-02-09T23:31:22Z")

</div>

Hi all, I’m building a portfolio site and the solution you’ve come up with seems like what I would need to achieve the attached UI. Where selecting and deselecting each of the filter buttons at the top would shrink or grow the list by showing content that matches all of the currently selected filters. I’m still in the design phase here am trying to work out if what I’m after is even possible. I’m thinking I would add a block above for each permutation that my taxonomy items create, then stash the URL until the selecting/deselecting matches one.

 ![45%20PM](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/2X/8/8e6ecca938a616c6f628c60a1196e49bfaf6c2d4.png)

---

<div class="post-metadata">

**Author:** ![Scherb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/scherb/32/4789_2.png) [@Scherb](https://discourse.gohugo.io/u/Scherb)\
**Post date:** [March 6, 2018, 5:44pm UTC](https://discourse.gohugo.io/t/solved-how-to-create-union-list-of-terms-of-two-taxonomies/9539/7 "2018-03-06T17:44:49Z")

</div>

Hi szac,

Probably to late to help! but… you may consider using javascript to achieve the filters depending on how many items are on the page. Since you are already printing them all out to the page to begin with and then filtering down. This would be easy by printing the taxonomies for each item as data attributes.

I guess if there are tons of the items you may look for a different approach.

---

<div class="post-metadata">

**Author:** ![szac](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/szac/32/5358_2.png) [@szac](https://discourse.gohugo.io/u/szac)\
**Post date:** [March 6, 2018, 6:24pm UTC](https://discourse.gohugo.io/t/solved-how-to-create-union-list-of-terms-of-two-taxonomies/9539/8 "2018-03-06T18:24:38Z")

</div>

Thanks Scherb

> [@Scherb](#):
>
> you may consider using javascript to achieve the filters

Yep, that’s pretty much what I ended up doing. My thinking here was for pagination and deep linking to search sets of multiple filters. This should be possible too with `pushState` but I haven’t messed around with it.

---

<div class="post-metadata">

**Author:** ![Scherb](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/scherb/32/4789_2.png) [@Scherb](https://discourse.gohugo.io/u/Scherb)\
**Post date:** [March 6, 2018, 6:34pm UTC](https://discourse.gohugo.io/t/solved-how-to-create-union-list-of-terms-of-two-taxonomies/9539/9 "2018-03-06T18:34:40Z")

</div>

Ok cool
