<p>If it works hard-coded, try trimming your variable:</p>

<p><code>set searchQuery = searchQuery|trim</code></p>

<p>OR, even better, build an array:</p>

<pre><code>{% set categories = [] %}

{% for category in currentUser.kb__category %}
   {% set categories = categories|merge(category.id) %}
{% endfor %}


{% for category in craft.categories
  .group('knowledgeBase')
  .id(categories)
  .level(1)
  .all()
%}
</code></pre>

CC BY-SA 4.0
2019-07-22T09:57:52.213Z
Sadly, neither of those worked. Building the array just results in an error trying to use merge when it return a string.
Christopher Dowson
CC BY-SA 4.0
2019-07-18T16:18:07.870Z
2019-07-18T16:27:10.290Z
2019-07-18T16:27:10.290Z
dmatthams
<p>I'm building a knowledgebase for a client. The client has asked for content to be restricted by category and then by user. This part is easy enough - the users now use the same category field as articles. However, the templating is proving troublesome.</p>

<p>Currently I'm querying for the category IDs, building an array and using it in the main category query.</p>

<pre><code>{% set searchQuery %}[{% for category in currentUser.kb__category %}{{ category.id }}{% if not loop.last %}, {% endif %}{% endfor %}]{% endset %}
</code></pre>

<p>This returns, for example, <code>[268, 270, 273, 276]</code></p>

<p>Consistently, the page is displaying all but the first category. This happens no matter what the first assigned category is.</p>

<pre><code>{% for category in craft.categories
  .group('knowledgeBase')
  .id(searchQuery)
  .level(1)
  .all()
%}
</code></pre>

<p>If I replace <code>searchQuery</code> with <code>[268, 270, 273, 276]</code> it works perfectly, which is baffling me as that's what has been output.</p>

<templating><categories>
Category query returning one fewer items than requested
# Category query returning one fewer items than requested

(Asked by: username_0 on 2019-07-18)

I'm building a knowledgebase for a client. The client has asked for content to be restricted by category and then by user. This part is easy enough - the users now use the same category field as articles. However, the templating is proving troublesome.
Currently I'm querying for the category IDs, building an array and using it in the main category query.
{% set searchQuery %}[{% for category in currentUser.kb__category %}{{ category.id }}{% if not loop.last %}, {% endif %}{% endfor %}]{% endset %}

This returns, for example, [268, 270, 273, 276]
Consistently, the page is displaying all but the first category. This happens no matter what the first assigned category is.
{% for category in craft.categories
  .group('knowledgeBase')
  .id(searchQuery)
  .level(1)
  .all()
%}

If I replace searchQuery with [268, 270, 273, 276] it works perfectly, which is baffling me as that's what has been output.

**Tags:** templating, categories

---

## 1 answers

### Answer by username_1 on 2019-07-18. Score: 1

If it works hard-coded, try trimming your variable:
set searchQuery = searchQuery|trim
OR, even better, build an array:
{% set categories = [] %}

{% for category in currentUser.kb__category %}
   {% set categories = categories|merge(category.id) %}
{% endfor %}


{% for category in craft.categories
  .group('knowledgeBase')
  .id(categories)
  .level(1)
  .all()
%}

> **Comments:**
> **username_0**: Sadly, neither of those worked. Building the array just results in an error trying to use merge when it return a string.  

---

