How do I get a list of taxonomy terms ordered by weight where the date is within the last year?


#1

Howdy. I’m using this code to show the ten most used terms on my site:

<ul class="tags rec">
	{{- $.Scratch.Set "limit" 0 }}
	{{- range $key, $value := .Data.Terms.ByCount }}
		{{- $.Scratch.Set "limit" (add ($.Scratch.Get "limit") 1) }}
		{{- if le ($.Scratch.Get "limit") 10 }}
	<li><a href="/tags/{{ $value.Name | urlize }}/">{{ $value.Name }}</a></li>
		{{- end }}
	{{- end }}
</ul>

Which is cool, but I was wondering if there was a way to limit it to the most used terms only in the last year? Can one use where to filter terms, then sort just that subset by counts?


#2

Not with one-liner code, I think, though I’m happy to be corrected on this.

With some $.Scratch-ing:

First get all tags and their dates

{{ $pages := where .Site.RegularPages "Type" "post" }} 
{{ $.Scratch.Set "tagsArr" (slice ) }}
{{ range $pages }}
  {{ if isset .Params "tags" }}
    {{ $d := .Date }}
    {{ range .Params.tags }}
      {{ $.Scratch.Add "tagsArr" (dict "tag" . "date" $d ) }}
    {{ end }}
  {{ end }}
{{ end }}

Then get just the tags for the current year

{{ $.Scratch.Set "tagCountsByYear" (dict ) }}
{{ range $k, $v := ($.Scratch.Get "tagsArr") }}
  {{ if eq (now.Format "2006") ($v.date.Format "2006") }}
  <!-- Count -->
    {{ if isset ($.Scratch.Get "tagCountsByYear") $v.tag }}
      {{ $c := index ( $.Scratch.Get "tagCountsByYear" ) $v.tag }}
      {{ $.Scratch.SetInMap "tagCountsByYear" $v.tag (add 1 $c ) }}
    {{ else }}
      {{ $.Scratch.SetInMap "tagCountsByYear" $v.tag 1 }}
    {{ end }}
  {{ end }}
{{ end }}

Get the tenth value when sorted descending

{{ $.Scratch.Set "tenth" 0 }}
{{ range $i, $e := sort ($.Scratch.Get "tagCountsByYear") "value" "desc" }}
  {{ if eq $i 9  }}
  {{ $.Scratch.Set "tenth" $e }}
  {{ end }}
{{ end }}

And finally show only those tags whose counts are greater than the tenth value

<ul>
{{ range $tag, $count := ($.Scratch.Get "tagCountsByYear") }}
  {{ if ge $count ($.Scratch.Get "tenth")  }}
    <li><a href="/tags/{{$tag | urlize}}">{{$tag}}</a> - {{$count}}</li>
  {{ end }}
{{ end }}
</ul>

You may need to change some of the code to fit your own, but give it a whirl. I’m hoping though someone else can say if a shorter / less verbose way is possible.


#3

Thank you! That gave me enough information to go on. Here’s what I came up with. Note that I’ve added a config param, Site.Params.recentTagYears to have some control over how many years back to look.

{{- $yearAgo := now.AddDate (int (mul -1 .Site.Params.recentTagYears)) 0 0 }}
{{- $pages := where .Site.RegularPages "Type" "in" .Site.Params.listTypes }}
{{- $.Scratch.Set "yearTags" (dict) }}
{{- range $pages }}
	{{- if gt .Date $yearAgo }}{{ range .Params.tags }}
		{{ if isset ($.Scratch.Get "yearTags") . }}
			{{- $c := index ( $.Scratch.Get "yearTags" ) . }}
			{{- $.Scratch.SetInMap "yearTags" . (dict "tag" . "cnt" (add 1 (index $c "cnt"))) }}
		{{- else }}
			{{- $.Scratch.SetInMap "yearTags" . (dict "tag" . "cnt" 1) }}
		{{- end }}
	{{- end }}{{ end }}
{{- end }}
{{- with $.Scratch.Get "yearTags" }}
<h4>Top Ten Recent Tags</h4>
<ul class="tags rec">
	{{- $.Scratch.Set "limit" 0 }}
	{{- range sort . ".cnt" "desc" }}
		{{- $.Scratch.Set "limit" (add ($.Scratch.Get "limit") 1) }}
		{{- if le ($.Scratch.Get "limit") 10 }}
	<li><a href="/tags/{{ .tag | urlize }}/">{{ .tag }} (({{ .cnt }})</a></li>
		{{- end }}
	{{- end }}
</ul>
{{- end }}