Answers.Body: <p><code>craft.categories</code> will return you an object of type <code>ElementCriteriaModel</code></p>
  
  <p><code>craft.categories.slug('what-we-do/' ~ category|default)</code> sets an attribute of this object, but it does not fetch the elements yet.</p>
  
  <p>The <code>relatedTo</code> <a href="https://craftcms.com/docs/relations#the-relatedTo-param" rel="nofollow noreferrer">parameter</a> needs to be an element, a list of elements, an array and some other staff but it can't handle a query (which the <code>ElementCriteriaModel</code> is) </p>
  
  <p>You have to fetch the element(s) before you pass it to the function with <code>first()</code>, <code>ids()</code>, <code>last()</code> or <code>find()</code></p>
  
  <pre><code>craft.entries.section(sections).relatedTo(craft.categories.slug('what-we-do/' ~ category|default).first())
  </code></pre>
  
  <p>Btw: just as an additional note <code>{{ cat | kebab }}</code> will print the title in kebab case. Mostly that will be the slug but if the user changes the title or the slug or inserts something completely different from the beginning, your logic will break </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-02-17T00:37:51.897Z
Answers.LastActivityDate: 2018-02-17T00:55:41.043Z
Answers.LastEditDate: 2018-02-17T00:55:41.043Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I have created a test category called <strong>test</strong> inside the <strong>what-we-do</strong> category group. There are three files at play</p>
  
  <p>The first has a line of code that generates the query string on button click</p>
  
  <pre><code>{% for cat in categories %}
      &lt;li class="vr"&gt;&lt;a href="{{ entry.url }}?category={{ cat | kebab }}" class="btn btn_plain mix-btn_noWidth"&gt;{{ cat.title }}&lt;/a&gt;&lt;/li&gt;
  {% endfor %}
  </code></pre>
  
  <p>The second takes the query string formats it and passes it to the list template. The code looks like</p>
  
  <pre><code>{% if craft.request.queryStringWithoutPath %}
      {% set categoryQueryString = craft.request.QueryStringWithoutPath| replace({"category=" : ""}) %} 
  {% else %}
      {% set categoryQueryString = "" %}        
  {% endif %}
  
  &lt;ul&gt;
      {% include '_includes/snippets/blog/list.html.twig' with {category : categoryQueryString} %}
  &lt;/ul&gt;
  </code></pre>
  
  <p>Then the list code looks like</p>
  
  <pre><code>{% set sections = 'blogPosts' %}
  {% paginate craft.entries.section(sections).relatedTo(craft.categories.slug('what-we-do/' ~ category|default)).limit(limit) as pageInfo, pageEntries %}
  
  {% for entry in pageEntries %}
      {% if entry['listingPageText']|default %}
          {% set content = entry.listingPageText %}
      {% endif %}
      &lt;div&gt;
          &lt;p&gt;{{ content }} &lt;/p&gt;
      &lt;/div&gt;
  {% end for %}
  </code></pre>
  
  <p>This section seems to be working when I dump it but I am not seeing any of my entries rendered</p>
  
  <pre><code>{% paginate craft.entries.section(sections).relatedTo(craft.categories.slug('what-we-do/' ~ category|default)).limit(limit) as pageInfo, pageEntries %}
  </code></pre>
  
  <p>I'm wondering if there is something simple i'm missing</p>
  
  <p>If I use</p>
  
  <pre><code>{% paginate craft.entries.section(sections).relatedTo(category|default).limit(limit) as pageInfo,pageEntries %}
  </code></pre>
  
  <p>I can get the posts to appear but it does not filter by category it just defaults and renders all the blog posts.</p>
  
  <p>I'm guessing that's because I have to pass the full category url what-we-do/test assuming the category name is test. </p>
  
  <p>What I really need is it to show the blogposts that are of category test inside the category group what-we-do</p>
  
Tags: <templating>
Title: Use query string with relatedTo() with categories inside a category group
ThreadText: # Use query string with relatedTo() with categories inside a category group
  
  (Asked by: username_0 on 2018-02-16)
  
  I have created a test category called **test** inside the **what-we-do** category group. There are three files at play
  
  The first has a line of code that generates the query string on button click
  
  ```
  {% for cat in categories %}
      <li class="vr"><a href="{{ entry.url }}?category={{ cat | kebab }}" class="btn btn_plain mix-btn_noWidth">{{ cat.title }}</a></li>
  {% endfor %}
  
  ```
  
  The second takes the query string formats it and passes it to the list template. The code looks like
  
  ```
  {% if craft.request.queryStringWithoutPath %}
      {% set categoryQueryString = craft.request.QueryStringWithoutPath| replace({"category=" : ""}) %} 
  {% else %}
      {% set categoryQueryString = "" %}        
  {% endif %}
  
  <ul>
      {% include '_includes/snippets/blog/list.html.twig' with {category : categoryQueryString} %}
  </ul>
  
  ```
  
  Then the list code looks like
  
  ```
  {% set sections = 'blogPosts' %}
  {% paginate craft.entries.section(sections).relatedTo(craft.categories.slug('what-we-do/' ~ category|default)).limit(limit) as pageInfo, pageEntries %}
  
  {% for entry in pageEntries %}
      {% if entry['listingPageText']|default %}
          {% set content = entry.listingPageText %}
      {% endif %}
      <div>
          <p>{{ content }} </p>
      </div>
  {% end for %}
  
  ```
  
  This section seems to be working when I dump it but I am not seeing any of my entries rendered
  
  ```
  {% paginate craft.entries.section(sections).relatedTo(craft.categories.slug('what-we-do/' ~ category|default)).limit(limit) as pageInfo, pageEntries %}
  
  ```
  
  I'm wondering if there is something simple i'm missing
  
  If I use
  
  ```
  {% paginate craft.entries.section(sections).relatedTo(category|default).limit(limit) as pageInfo,pageEntries %}
  
  ```
  
  I can get the posts to appear but it does not filter by category it just defaults and renders all the blog posts.
  
  I'm guessing that's because I have to pass the full category url what-we-do/test assuming the category name is test.
  
  What I really need is it to show the blogposts that are of category test inside the category group what-we-do
  
  **Tags:** templating
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-02-17. Score: 1 | Accepted answer
  
  `craft.categories` will return you an object of type `ElementCriteriaModel`
  
  `craft.categories.slug('what-we-do/' ~ category|default)` sets an attribute of this object, but it does not fetch the elements yet.
  
  The `relatedTo` [parameter](https://craftcms.com/docs/relations#the-relatedTo-param) needs to be an element, a list of elements, an array and some other staff but it can't handle a query (which the `ElementCriteriaModel` is)
  
  You have to fetch the element(s) before you pass it to the function with `first()`, `ids()`, `last()` or `find()`
  
  ```
  craft.entries.section(sections).relatedTo(craft.categories.slug('what-we-do/' ~ category|default).first())
  
  ```
  
  Btw: just as an additional note `{{ cat | kebab }}` will print the title in kebab case. Mostly that will be the slug but if the user changes the title or the slug or inserts something completely different from the beginning, your logic will break
  
  
  ---
  
  