Body: <p>I'm trying to display results from a search. I want to display the categories represented by the search results and the entry count for that category. Think of how Amazon's "refine results" sidebar works, something like:</p>
  
  <blockquote>
    <p><strong>Results listing</strong>
    ...</p>
    
    <p><strong>Categories</strong>
    <br>Planning and Funding (9)<br>
    Design (19)<br>
    etc.</p>
  </blockquote>
  
  <p>I can easily get the count of <em>ALL ENTRIES</em> in each category with this:</p>
  
  <pre><code>{% for category in craft.categories.group('resourceCategories') %}
    {% set entryCount = craft.entries.relatedTo(category).total() %}
    ... do something...
  {% endfor %}
  </code></pre>
  
  <p>However, if I want to get counts for ONLY the returned search results. I've tried this:</p>
  
  <pre><code>{% set resourceEntries = craft.entries(queryParams).section('and, not organizations, not presentersAndAuthors') %}
  {% for category in craft.categories.group('resourceCategories') %}
   {% set entryCount = resourceEntries.relatedTo(category).total() %}
    ... do something...
  {% endfor %}
  </code></pre>
  
  <p>But I get the count for ALL entries (the same results as the top example).</p>
  
  <p>Any idea as to what am I missing or not understanding?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-23T21:28:11.600Z
Comments.Text: Did you try to create a new query instead of executing the same object again and again? Also what are your `queryParams`
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-01-23T22:21:50.470Z
Comments.Text: I did try creating a new query. Same results. The queryParam variable is built with some basic defaults (order, subLeft, subRight) and then a relatedTo call from merged with a category ids. like so:   {% set queryParams = queryParams|merge({
      relatedTo: categoriesArray   }) %}
  Which means there is a relatedTo inside the queryParams and I'm wondering if that is the issue. Maybe my question should be, how to get a category count for a craft.entries object? I assume stepping through each entry and building an array is not the most efficient way?
Comments.UserDisplayName: Mosswalker
Tags: <categories><search><relatedto><query>
Title: Category counts for query results
ThreadText: # Category counts for query results
  
  (Asked by: username_0 on 2018-01-23)
  
  I'm trying to display results from a search. I want to display the categories represented by the search results and the entry count for that category. Think of how Amazon's "refine results" sidebar works, something like:
  
  > **Results listing**
  > ...
  >
  > **Categories**
  >   
  > Planning and Funding (9)  
  > Design (19)  
  > etc.
  
  I can easily get the count of *ALL ENTRIES* in each category with this:
  
  ```
  {% for category in craft.categories.group('resourceCategories') %}
    {% set entryCount = craft.entries.relatedTo(category).total() %}
    ... do something...
  {% endfor %}
  
  ```
  
  However, if I want to get counts for ONLY the returned search results. I've tried this:
  
  ```
  {% set resourceEntries = craft.entries(queryParams).section('and, not organizations, not presentersAndAuthors') %}
  {% for category in craft.categories.group('resourceCategories') %}
   {% set entryCount = resourceEntries.relatedTo(category).total() %}
    ... do something...
  {% endfor %}
  
  ```
  
  But I get the count for ALL entries (the same results as the top example).
  
  Any idea as to what am I missing or not understanding?
  
  **Tags:** categories, search, relatedto, query
  
  > **Comments:**
  > **username_1**: Did you try to create a new query instead of executing the same object again and again? Also what are your `queryParams`  
  > **username_0**: I did try creating a new query. Same results. The queryParam variable is built with some basic defaults (order, subLeft, subRight) and then a relatedTo call from merged with a category ids. like so:   {% set queryParams = queryParams|merge({     relatedTo: categoriesArray   }) %} Which means there is a relatedTo inside the queryParams and I'm wondering if that is the issue. Maybe my question should be, how to get a category count for a craft.entries object? I assume stepping through each entry and building an array is not the most efficient way?  
  
  ---
  
  ## 0 answers
  
  