Answers.Body: <p>Short answer: There is no way to apply this sort of logic to the <code>craft.categories</code> query. The way I have set this up is good. It can be improved performance wise by wrapping the queries around a <code>{% cache %}</code> tag.</p>
  
  <p>As Carl has pointed out in <a href="https://craftcms.stackexchange.com/a/11159/176">his answer</a>, the matching categories could also be merged into an array and added to a new query, which could then be used for pagination etc.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-25T10:43:14.437Z
Answers.LastActivityDate: 2015-08-25T10:43:14.437Z
Answers.LastEditDate: 2017-04-13T12:51:00.200Z
Answers.UserDisplayName: Katrin
Answers.Body: <p>What you can always do in situations, where the default parameters are not enough,
  is to "collect" element IDs and pass them to the <code>id</code> param of a new criteria model.</p>
  
  <p>So loop through all categories and merge the category IDs matching you condition to an array:</p>
  
  <pre><code>{% set topCategoriesIds = mainCategoriesIds|merge([category.id]) %}
  </code></pre>
  
  <p>When you're done, get your filtered categories from these IDs:</p>
  
  <pre><code>{% set categories = craft.categories.id(topCategoriesIds) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-25T07:54:47.827Z
Answers.Text: Thank you Carl. I don't fully follow. How does that give me categories that are related to equal or more than 4 entries? If I understand correctly, this still creates an extra loop and query, so performance wise there is no gain to my original way, is there?
Answers.UserDisplayName: Katrin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-25T08:00:27.057Z
Answers.Text: No, it is really the same to your idea, so no performance gains at all. The only difference is, that you can now query for them with a single criteria model. I thought that's what you needed, as this allows you to paginate the results or do other things not possible when having the conditional in your "output loop".
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-25T08:14:29.470Z
Answers.Text: That makes sense and am sure would come in very handy in other scenarios. For my current situation, I was looking to see if there was a way to adapt the `craft.categories` query so that I didn't have to create an additional entries query and loop.
Answers.UserDisplayName: Katrin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-25T08:21:26.943Z
Answers.Text: No, unfortunatelly there's no way to add your custom conditional to the `relatedTo` param in your query. What about wrapping the output loop in `cache` tags? How is your PHP/MySQL knowledge, you could write a custom plugin to get them with a single query from the database?
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-25T10:31:02.393Z
Answers.Text: Yes, definitely a good call on the caching. My PHP knowledge is limited. I could maybe dabble something together, but it isn't absolutely necessary right now and am happy to use what I've got. Thanks for your ideas :)
Answers.UserDisplayName: Katrin
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-24T21:20:20.090Z
Answers.LastActivityDate: 2015-08-24T21:20:20.090Z
Answers.UserDisplayName: carlcs
Body: <p>Is there a way to only fetch categories that have been assigned to a minimum of entries, i.e. <code>4</code>?</p>
  
  <p>Currently I fetch categories like this:</p>
  
  <pre><code>{% set productIds = craft.entries.section('products').ids() %}
  {% set categories = craft.categories.relatedTo({ sourceElement: productIds }) %}
  </code></pre>
  
  <p>I know I can now do the following to only display content for categories which have been assigned to at least  four entries:</p>
  
  <pre><code>{% set entries = craft.entries.section('products').relatedTo(category) %}
  {% if entries|length &gt; 4 %}
    ...
  {% endif %}
  </code></pre>
  
  <p>But is there a way to apply this logic to the <code>craft.categories</code> query?</p>
  
Tags: <categories><relations>
Title: Only fetch categories that are related to a minimum of entries
ThreadText: # Only fetch categories that are related to a minimum of entries
  
  (Asked by: username_0 on 2015-08-24)
  
  Is there a way to only fetch categories that have been assigned to a minimum of entries, i.e. `4`?
  
  Currently I fetch categories like this:
  
  ```
  {% set productIds = craft.entries.section('products').ids() %}
  {% set categories = craft.categories.relatedTo({ sourceElement: productIds }) %}
  
  ```
  
  I know I can now do the following to only display content for categories which have been assigned to at least four entries:
  
  ```
  {% set entries = craft.entries.section('products').relatedTo(category) %}
  {% if entries|length > 4 %}
    ...
  {% endif %}
  
  ```
  
  But is there a way to apply this logic to the `craft.categories` query?
  
  **Tags:** categories, relations
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2015-08-25. Score: 1 | Accepted answer
  
  Short answer: There is no way to apply this sort of logic to the `craft.categories` query. The way I have set this up is good. It can be improved performance wise by wrapping the queries around a `{% cache %}` tag.
  
  As Carl has pointed out in [his answer](https://craftcms.stackexchange.com/a/11159/176), the matching categories could also be merged into an array and added to a new query, which could then be used for pagination etc.
  
  
  ---
  
  ### Answer by username_1 on 2015-08-24. Score: 1
  
  What you can always do in situations, where the default parameters are not enough,
  is to "collect" element IDs and pass them to the `id` param of a new criteria model.
  
  So loop through all categories and merge the category IDs matching you condition to an array:
  
  ```
  {% set topCategoriesIds = mainCategoriesIds|merge([category.id]) %}
  
  ```
  
  When you're done, get your filtered categories from these IDs:
  
  ```
  {% set categories = craft.categories.id(topCategoriesIds) %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you Carl. I don't fully follow. How does that give me categories that are related to equal or more than 4 entries? If I understand correctly, this still creates an extra loop and query, so performance wise there is no gain to my original way, is there?  
  > **username_1**: No, it is really the same to your idea, so no performance gains at all. The only difference is, that you can now query for them with a single criteria model. I thought that's what you needed, as this allows you to paginate the results or do other things not possible when having the conditional in your "output loop".  
  > **username_0**: That makes sense and am sure would come in very handy in other scenarios. For my current situation, I was looking to see if there was a way to adapt the `craft.categories` query so that I didn't have to create an additional entries query and loop.  
  > **username_1**: No, unfortunatelly there's no way to add your custom conditional to the `relatedTo` param in your query. What about wrapping the output loop in `cache` tags? How is your PHP/MySQL knowledge, you could write a custom plugin to get them with a single query from the database?  
  > **username_0**: Yes, definitely a good call on the caching. My PHP knowledge is limited. I could maybe dabble something together, but it isn't absolutely necessary right now and am happy to use what I've got. Thanks for your ideas :)  
  
  ---
  
  