1

I have got multiple category groups and I need to concatenate them into one list for a filter UI element.

At the moment I have the following:

{% set categories = craft.categories.group('colour').all() %}
{% set categories = categories|merge(craft.categories.group('suitability').all()) %}
{% set categories = categories|merge(craft.categories.group('finish').all()) %}
{% set categories = categories|merge(craft.categories.group('shape').all()) %}
{% set categories = categories|merge(craft.categories.group('material').all()) %}
{% set categories = categories|merge(craft.categories.group('edge').all()) %}
{% set categories = categories|merge(craft.categories.group('surface').all()) %}

So now I have a list of all the categories within these groups, I can loop over them as follows:

<ul class="categories">
    {% for cat in categories %}
        <li class="categories__item">
            <button class="button categories__btn">{{ cat.title }}</button>
        </li>
    {% endfor %}
</ul>

But the issue I'm having is that the array of categories is outputting them in the order they're being added. I tried this:

{% set categories = categories.orderBy('title asc').all() %} 

But it returns "Impossible to invoke a method ("orderBy") on an array."

2

You can query on multiple category groups like so:

craft.categories.group('suitability, finish, shape, material, edge, surface').all()

It can also be done with an array:

craft.categories.group([
  'suitability',
  'finish',
  'shape',
  'material',
  'edge',
  'surface'
]).all()

Whenever you invoke .all() it returns an array. As those arrays were merged, they created a larger array. In that last bit of code, categories.orderBy() failed because categories was an array and arrays do not have a method named orderBy.

Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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