1

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?

  • Did you try to create a new query instead of executing the same object again and again? Also what are your queryParams – Robin Schambach Jan 23 '18 at 21:28
  • 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? – Mosswalker Jan 23 '18 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.