<p>Your current query queries for all categories in the category group &quot;title&quot;, ordering them by their title.</p>
<p>If you want to list three specific categories, you'll have to create a <a href="https://craftcms.com/docs/4.x/categories.html#querying-categories" rel="nofollow noreferrer">categories query</a> that filters out all the other categories somehow.</p>
<p>In Craft, most fields can be used to filter element query results. For example, if you wanted to filter by the categories' <em>title</em>, you could use the <code>.title()</code> param – the below query would only return categories with a title &quot;Foo&quot;, &quot;Bar&quot; or &quot;Baz&quot;:</p>
<pre><code>{% set categories = craft.categories.group('title').title(['Foo', 'Bar', 'Baz']).all() %}
</code></pre>
<p>You can also query by category IDs:</p>
<pre><code>{% set categories = craft.categories.group('title').id([125, 43624, 34]).all() %}
</code></pre>
<p>Or even by a custom field – for example, assuming you had a Lightswitch field called <code>featured</code>, and wanted to query for all categories with this Lightswitch field toggled on:</p>
<pre><code>{% set categories = craft.categories.group('title').featured(1).all() %}
</code></pre>
<p>Another approach entirely is using a Categories field, to enable authors to select which categories to display. For example, if you created a categories field called <code>someCategoriesField</code>, and added it to a global set <code>someGlobalSet</code>, you could do this to return any and all categories selected in that field:</p>
<pre><code>{% set categories = someGlobalSet.someCategoriesField.all() %}
</code></pre>
<p>If you want to make sure that only 3 categories are returned, you might also consider adding a <code>.limit()</code> parameter to your query:</p>
<pre><code>{% set categories = craft.categories.group('title').limit(3).all() %}
</code></pre>

CC BY-SA 4.0
2022-12-06T08:52:00.287Z
2022-12-07T19:32:27.407Z
2022-12-07T19:32:27.407Z
Mats Mikkel Rummelhoff
<p>I have a categories group named &quot;Title&quot; in my backend. In that group I have 15 categories, but I want to display only 3 specific categories on the frontend. How can I filter my query and display only certain categories?</p>
<pre><code>{% for category in craft.categories({ group: 'title', orderBy: 'title asc' }).all() %}
    {{ category.title }} {# will display all the categories but I want to display only 3 specific categories #}
{% endfor %}
</code></pre>

<craft3><categories><category><filtering><category-filter>
How to filter a categories query
# How to filter a categories query

(Asked by: username_0 on 2022-12-06)

I have a categories group named "Title" in my backend. In that group I have 15 categories, but I want to display only 3 specific categories on the frontend. How can I filter my query and display only certain categories?
{% for category in craft.categories({ group: 'title', orderBy: 'title asc' }).all() %}
    {{ category.title }} {# will display all the categories but I want to display only 3 specific categories #}
{% endfor %}

**Tags:** craft3, categories, category, filtering, category-filter

---

## 1 answers

### Answer by username_1 on 2022-12-06. Score: 3

Your current query queries for all categories in the category group "title", ordering them by their title.
If you want to list three specific categories, you'll have to create a categories query that filters out all the other categories somehow.
In Craft, most fields can be used to filter element query results. For example, if you wanted to filter by the categories' title, you could use the .title() param – the below query would only return categories with a title "Foo", "Bar" or "Baz":
{% set categories = craft.categories.group('title').title(['Foo', 'Bar', 'Baz']).all() %}

You can also query by category IDs:
{% set categories = craft.categories.group('title').id([125, 43624, 34]).all() %}

Or even by a custom field – for example, assuming you had a Lightswitch field called featured, and wanted to query for all categories with this Lightswitch field toggled on:
{% set categories = craft.categories.group('title').featured(1).all() %}

Another approach entirely is using a Categories field, to enable authors to select which categories to display. For example, if you created a categories field called someCategoriesField, and added it to a global set someGlobalSet, you could do this to return any and all categories selected in that field:
{% set categories = someGlobalSet.someCategoriesField.all() %}

If you want to make sure that only 3 categories are returned, you might also consider adding a .limit() parameter to your query:
{% set categories = craft.categories.group('title').limit(3).all() %}


---

