Answers.Body: <p>If I understand you correctly, you're looking to build a <a href="https://en.wikipedia.org/wiki/Faceted_search" rel="nofollow noreferrer">faceted search</a>, right? So the available filters (checkboxes in this case) are reduced based on the currently active filters to only show those that would yield any results.</p>
  <p>In your setup, the key to that is to get the current result set and then limit the category query to only categories that are <code>relatedTo</code> the current result set. If you have a paginated results page, you may need two entry queries. One to get the results for the current page, and another to get the IDs of all results to use for the <code>relatedTo</code> parameter for the category queries.</p>
  <p>You don't show the code you're using to filter results, but you're probably using the <code>industry</code> parameter to limit the search results, right? In this case, you only need to adjust the query you're using to get the array of <code>entryIds</code> to include that <code>industry</code> filter as well (and any other filters you may have).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-01T09:41:40.717Z
Answers.Text: @ElektraMurphy Please edit your question to post code snippets, it's really hard to read in the comments ^^
Answers.UserDisplayName: MoritzLost
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-01T09:46:48.673Z
Answers.Text: You're right, apologies, I amended by adding in the post above.
Answers.UserDisplayName: Elektra Murphy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-01T09:28:50.200Z
Answers.LastActivityDate: 2022-02-01T09:28:50.200Z
Answers.UserDisplayName: MoritzLost
Answers.Body: <p>I was able to do something similar by bringing in the <a href="https://github.com/besteadfast/craft-preparse-field/blob/master/README.md" rel="nofollow noreferrer">Preparse Field</a> plugin.</p>
  <p>Within the category, add a preparse field to count the number of related entries.
  {{craft.entries().section('blog').relatedTo(element).count() }}</p>
  <p>And then, in the template, only display the category if the preparse field shows some number of relationships.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-18T20:33:00.653Z
Answers.LastActivityDate: 2022-10-18T20:33:00.653Z
Answers.UserDisplayName: user2208757
Body: <p>I've built three buttons with checkbox dropdowns that display categories that filter the content below based on categories. I am currently using the relatedTo function to only display the categories that have any content tagged. However, what I'm trying to achieve is to have the category checkboxes be hidden in real time if there's no related entry to them.</p>
  <p>I've read the docs and cannot seem to find a solution. My main question is whether this is possible without JavaScript and only using Twig? If it is not I may have to re-think the project structure.</p>
  <pre><code>{% set entryIds = craft.entries.section(['blog', 'guides', 'webinars', 'podcast']).ids() %}
  
  {% set blogOptions = craft.categories.group('categories').relatedTo({ sourceElement: entryIds }).find() %}
  {% set industryOptions = craft.categories.group('industry').relatedTo({ sourceElement: entryIds }).find() %}
  {% set formatOptions = craft.categories.group('format').relatedTo({ sourceElement: entryIds }).find() %}
  
  {# Generate a product query, eager-loading relationship fields. #} 
  {% set productQuery = craft.entries.section(['blog', 'guides', 'webinars', 'podcast']) %} 
  
  {# Get products from query. #} 
  {% set products = productQuery.offset(offset).limit(limit).orderBy('postDate desc').all() %} 
  
  
  {# ONE of the three checkbox filters #}
  &lt;div id=&quot;IndustryTable&quot; class=&quot;TableBody&quot; style=&quot;display:none&quot;&gt;
   {% for industryOption in industryOptions %}
      &lt;div class=&quot;subcategories&quot;&gt;
          &lt;input sprig type=&quot;checkbox&quot; id=&quot;industry-{{ industryOption.id }}&quot; name=&quot;industry[]&quot; value=&quot;{{ industryOption.id }}&quot; {{industryOption.id in industry ? 'checked' }}&gt;
            &lt;label for=&quot;industry-{{ industryOption.id }}&quot; class=&quot;text-sm leading-5 font-medium text-cool-gray-500 truncate ml-1&quot;&gt;
                 {{ industryOption.title }}
             &lt;/label&gt;
         &lt;/div&gt;
     {% endfor %}
  &lt;/div&gt;
  
  {% for product in products %} 
  
  CONTENT 
  
  {% endfor %}
  
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-02-01T09:49:21.313Z
Comments.Text: Looks like you're not applying the `industry` parameter to the query? How are you using the filter values?
Comments.UserDisplayName: MoritzLost
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-02-01T09:56:33.473Z
Comments.Text: @MoritzLost I'm using sprig (https://putyourlightson.com/plugins/sprig) to update the content with s-replace when a checkbox category is clicked. On checked it reloads the content and only displays the content with the category tags selected.
Comments.UserDisplayName: Elektra Murphy
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-02-01T10:08:03.727Z
Comments.Text: Yes, but you're somehow applying the `industry` filter (and the other filters) to the product query, right? Otherwise, the filter would have no effect … anyway, once the sprig component is reloaded with an active filter you will have access to a `industry` variable containing the selected industries, apply that to the query you're using for the `entryIds` so the `industryOptions` will be limited to those related to one of the selected industries.
Comments.UserDisplayName: MoritzLost
Tags: <categories><search><filter><javascript><plugin-sprig>
Title: Hiding categories in real time with Sprig in a faceted search
ThreadText: # Hiding categories in real time with Sprig in a faceted search
  
  (Asked by: username_0 on 2022-02-01)
  
  I've built three buttons with checkbox dropdowns that display categories that filter the content below based on categories. I am currently using the relatedTo function to only display the categories that have any content tagged. However, what I'm trying to achieve is to have the category checkboxes be hidden in real time if there's no related entry to them.
  
  I've read the docs and cannot seem to find a solution. My main question is whether this is possible without JavaScript and only using Twig? If it is not I may have to re-think the project structure.
  
  ```
  {% set entryIds = craft.entries.section(['blog', 'guides', 'webinars', 'podcast']).ids() %}
  
  {% set blogOptions = craft.categories.group('categories').relatedTo({ sourceElement: entryIds }).find() %}
  {% set industryOptions = craft.categories.group('industry').relatedTo({ sourceElement: entryIds }).find() %}
  {% set formatOptions = craft.categories.group('format').relatedTo({ sourceElement: entryIds }).find() %}
  
  {# Generate a product query, eager-loading relationship fields. #} 
  {% set productQuery = craft.entries.section(['blog', 'guides', 'webinars', 'podcast']) %} 
  
  {# Get products from query. #} 
  {% set products = productQuery.offset(offset).limit(limit).orderBy('postDate desc').all() %} 
  
  {# ONE of the three checkbox filters #}
  <div id="IndustryTable" class="TableBody" style="display:none">
   {% for industryOption in industryOptions %}
      <div class="subcategories">
          <input sprig type="checkbox" id="industry-{{ industryOption.id }}" name="industry[]" value="{{ industryOption.id }}" {{industryOption.id in industry ? 'checked' }}>
            <label for="industry-{{ industryOption.id }}" class="text-sm leading-5 font-medium text-cool-gray-500 truncate ml-1">
                 {{ industryOption.title }}
             </label>
         </div>
     {% endfor %}
  </div>
  
  {% for product in products %} 
  
  CONTENT 
  
  {% endfor %}
  
  ```
  
  **Tags:** categories, search, filter, javascript, plugin-sprig
  
  > **Comments:**
  > **username_1**: Looks like you're not applying the `industry` parameter to the query? How are you using the filter values?  
  > **username_0**: @username_1 I'm using sprig (https://putyourlightson.com/plugins/sprig) to update the content with s-replace when a checkbox category is clicked. On checked it reloads the content and only displays the content with the category tags selected.  
  > **username_1**: Yes, but you're somehow applying the `industry` filter (and the other filters) to the product query, right? Otherwise, the filter would have no effect … anyway, once the sprig component is reloaded with an active filter you will have access to a `industry` variable containing the selected industries, apply that to the query you're using for the `entryIds` so the `industryOptions` will be limited to those related to one of the selected industries.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-02-01. Score: 1
  
  If I understand you correctly, you're looking to build a [faceted search](https://en.wikipedia.org/wiki/Faceted_search), right? So the available filters (checkboxes in this case) are reduced based on the currently active filters to only show those that would yield any results.
  
  In your setup, the key to that is to get the current result set and then limit the category query to only categories that are `relatedTo` the current result set. If you have a paginated results page, you may need two entry queries. One to get the results for the current page, and another to get the IDs of all results to use for the `relatedTo` parameter for the category queries.
  
  You don't show the code you're using to filter results, but you're probably using the `industry` parameter to limit the search results, right? In this case, you only need to adjust the query you're using to get the array of `entryIds` to include that `industry` filter as well (and any other filters you may have).
  
  > **Comments:**
  > **username_1**: @username_0 Please edit your question to post code snippets, it's really hard to read in the comments ^^  
  > **username_0**: You're right, apologies, I amended by adding in the post above.  
  
  ---
  
  ### Answer by username_2 on 2022-10-18. Score: 0
  
  I was able to do something similar by bringing in the [Preparse Field](https://github.com/besteadfast/craft-preparse-field/blob/master/README.md) plugin.
  
  Within the category, add a preparse field to count the number of related entries.
  {{craft.entries().section('blog').relatedTo(element).count() }}
  
  And then, in the template, only display the category if the preparse field shows some number of relationships.
  
  
  ---
  
  