HUGO

Filter posts in a json file

I have this JSON file I am using to create posts for a search page based on this guide. Inside the posts folder, I would like to either limit the posts shown by type: post or exclude a folder inside content/posts called ‘views’. I would appreciate some help on how to rework this code to achieve that. Thanks in advance.

[
  {{- with $.Site.GetPage "/posts" }}
    {{- range $index, $data := .Pages }}
    {{- if $index }},{{ end }}
    {
      "title": "{{ $data.Title  }}",
      "url": "{{ $data.RelPermalink }}"
    }
    {{- end }}
  {{- end }}
]

See the where function:

For example create a variable that holds the filtered page group -as per your requirement-

[
{{ $pages :=  where .Pages "Section" "!=" "views" }}
{{- range $index, $data := $pages }}
...
{{- end }}
]

Thanks! This was not displaying any results $pages := where .Pages "Section" "!=" "views" only displaying folder names that weren’t excluded. This is the part I needed {{- range $index, $data := $pages }} and used {{ $pages := where site.RegularPages "Type" "posts"}} . All solved now.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.