Filtering, sort, and limit tags in post context

I would like to show the tags on my posts on the post pages. However my posts have too many tags and they are not ordered coherently. Thus I’d like to filter, sort, and limit my tags.

Currently I’m doing:

{{#foreach tags}}
    <a class="..." href="{{url}}">
        {{name}}
    </a>
{{/foreach}}

Which outputs all of the tags of the post, in an order I don’t particularly care for.

I have tried:

{{#get "tags" limit="5" include="count.posts" order="count.posts desc" filter="slug:-[...]"}}
    {{#foreach tags}}
        <a class="..." href="{{url}}">
            {{name}}
        </a>
    {{/foreach}}
{{/get}}

But this approach returns all of the tags on my site as the result of get, not only the tags in the current post context!

How to apply filtering, sorting, and limiting to the tags of the current post only?

The default order of the tags is based on the order you specify in the post editor - so if you don’t like the order, you should be able to rearrange it by dragging/dropping them there :slight_smile:

w.r.t limiting - you’ll want to use the limit attribute

In my use case I’d like the tag ordering to be dynamic, based on the post count of each tag. You can see how I tried to implement this with get using order="count.posts desc". Manually ordering does not solve this.

Furthermore I need to be able to exclude certain tags. Again you can see how I have filter="slug:-[...]" in my get code.

Is this not possible? It seems unlikely to me that there’s no generic solution where we can sort and filter anything that’s iterable with foreach. But what is the syntax for it?

As far as I’m aware, once you’ve hit a foreach loop, you can’t sort the data, but you can filter using the has or match helpers.

The get helper is usually used to fetch new data, not to filter/sort existing data. I don’t think there’s a simple way to reorder tags