Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-22T09:57:52.213Z
Answers.Text: Sadly, neither of those worked. Building the array just results in an error trying to use merge when it return a string.
Answers.UserDisplayName: Christopher Dowson
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-18T16:18:07.870Z
Answers.LastActivityDate: 2019-07-18T16:27:10.290Z
Answers.LastEditDate: 2019-07-18T16:27:10.290Z
Answers.UserDisplayName: dmatthams
Body: <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>
  
Tags: <templating><categories>
Title: Category query returning one fewer items than requested
ThreadText: # 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.  
  
  ---
  
  