How can I filter pages by tag?

I have a couple sites that I’d like to have major sections on the homepage for certain tags. For example, in my Coffee Log (source is here) I might have to have an index that filtered by continent. I know I could use a separate parameter for this, but I’d like to play with the page a little with existing tags before I bite the bullet and add a separate parameter value everywhere.

I tried putting this in the main index (just to play with this idea:)

                   {{ range where .Data.Pages "Params.tags" "ethiopia" }}
                     {{ template "chrome/roast.html" . }}
                   {{ end }}

and it found nothing. I figure the problem is that tags is an array, but as far as I can tell there’s no “contains” operator.


hugo has in

but to what you’re trying to accomplish, you might be happier with Related Content because it’s very efficient:

In doesn’t work where the value isn’t a list, as far as I can tell.

You can try slice, but also check out the templating docs on taxonomies. might be something there. But, the Related Content stuff is likely your best bet.

No, related content is not really what I want, though it is a neat feature and I’ve tried it out.

well I can tell you it does precisely what you say you want to accomplish in your original post. So I must not understanding what you’re after.

This works, thanks:

                   {{ $p := slice "africa" }}
                   {{ range where .Data.Pages.ByTitle "Params.tags" "intersect" $p}}

If interested, my plan is to make a landing page where there is a separate list of a certain (but not all) tags. So for example in one of my repos I intend to make a “reference” section, a “projects” section, etc.

I don’t really care to automatically generate related posts to the current post as much as large sections for my corpus. The above is easier to understand and does precisely what I want, and no more, with no other configuration.

I understand, and that’s a good approach (intersect). Not to be so adamant about it (the difference is really just performance, so if you don’t notice it, it’s not important), but this would be the same thing (I think, untested) for Related: {{$foo := .Site.RegularPages.RelatedTo (keyVals "tags" "tagname")}}


huh, you’re right, it’s exactly the same output! I do sorta still prefer my way, only if it’s because it’s clearly just two operations, but this is good to know.

1 Like

Thanks for the fix! Exactly what I was looking for!