<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>

CC BY-SA 4.0
2022-02-01T09:41:40.717Z
@ElektraMurphy Please edit your question to post code snippets, it's really hard to read in the comments ^^
MoritzLost
CC BY-SA 4.0
2022-02-01T09:46:48.673Z
You're right, apologies, I amended by adding in the post above.
Elektra Murphy
CC BY-SA 4.0
2022-02-01T09:28:50.200Z
2022-02-01T09:28:50.200Z
MoritzLost
<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>

CC BY-SA 4.0
2022-10-18T20:33:00.653Z
2022-10-18T20:33:00.653Z
user2208757
<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>

CC BY-SA 4.0
2022-02-01T09:49:21.313Z
Looks like you're not applying the `industry` parameter to the query? How are you using the filter values?
MoritzLost
CC BY-SA 4.0
2022-02-01T09:56:33.473Z
@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.
Elektra Murphy
CC BY-SA 4.0
2022-02-01T10:08:03.727Z
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.
MoritzLost
<categories><search><filter><javascript><plugin-sprig>
Hiding categories in real time with Sprig in a faceted search
# 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 #}

 {% for industryOption in industryOptions %}
    
        
          
               {{ industryOption.title }}
           
       
   {% endfor %}


{% 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**: @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.  
> **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**: @ElektraMurphy 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.


---

