Looking to loop through tags from entries but limit to only show tags that are being used on entries. Right now this just loops through any and all created tags.

I tried limit to null but have had no luck getting that to work.

{% set categories = craft.categories.group('storiesCategories').find %}
{% set tags = craft.tags.group('storiesTagGroup').find %}
{% set elements = categories|merge(tags) %}

<ul class="pills">
    <li><a href="javascript:void(0);" class="btn btn-secondary filter" data-filter="all">All</a></li>

    {% for element in elements | supersort('natcasesort') %}
        <li><a href="javscript:void(0)" class="btn btn-secondary filter" data-filter=".category-{{ element.title|kebab }}">{{ element.title|capitalize }}</a></li>
    {% endfor %}

1 Answer 1


Your mention tags, but it seems like you're merging Categories and Tags together and trying to loop through the results.

If you leave Categories out of the equation it's as simple as this.

If you want Categories and Tags, you'd repeat that process twice and merge the results.

Note that using |merge on full-fledged arrays of objects is sketchy, though, since it's difficult for PHP to know how to compare the objects for equality. You'll be much better off merging on something primitive, like an array of element IDs.

Something like:

{% set categoryIds = craft.categories.group('storiesCategories').ids() %}
{% set tagIds = craft.tags.group('storiesTagGroup').ids() %}
{% set elementIds = categoryIds|merge(tagIds) %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.