8

Previously on EE I've used Low Search to generate a filter based search form, based on categories. But with Craft I can't seem to find an add-on - Or even know if I need one!

What I'm trying to achieve is a filer menu with categories e.g.:

[ ] Category 1
[x] Category 2
[ ] Category 3
[x] Category 4
   [ ] Category 4.1
   [ ] Category 4.2
[ ] Category 5

I've been reading over the documentation over at:

https://craftcms.com/docs/searching and https://craftcms.com/docs/templating/search-form

But I'm struggling to see how I can make this work within a search form itself.

I'm thinking I can generate a query fine to search the categories. But I can't work out how to link what is checked with a query.

Any help would be gooooood :)

9

I managed to figure this out by looking through a few other posts:

Which I then took their knowledge and put together my own form and results template. With the results I wanted the same code & template to be used for search results AND the general "knowledge" page.

Filter Form

<form action="{{ url('knowledge') }}">

    <ul>

        {% set categoryEntries = craft.entries.limit(null) %}

        {% for category in craft.categories.group('knowledge') %}

            {% set categoriesEntryCount = categoryEntries.relatedTo(category).total() %}

            <li>
              <input type="checkbox" name="category[]" value="{{ category.slug }}" id="{{ category.slug }}">
              <label for="{{ category.slug }}">{{ category.title }} ({{ categoriesEntryCount }})</label>
            </li>

        {% endfor %}

    </ul>

</form>

Results

    {% set parameters =
        {
            section: 'channelKnowledge',
            limit: '1'
        }
    %}

    {% set categoryQuery = craft.request.getParam('category') %}

    {% if categoryQuery %}
        {% set categoryQueryCategories = craft.categories.group('knowledge').slug(categoryQuery) %}
        {% set entries = craft.entries(parameters).relatedTo(categoryQueryCategories).order('score') %}
    {% else %}
        {% set entries = craft.entries(parameters) %}
    {% endif %}

    {% paginate entries as channelKnowledgeEntries %}

    {% if entries|length %}

        {% for entry in channelKnowledgeEntries %}

            {{ entry.title }}

        {% endfor %}

    {% else %}

        <p>No Results</p>

    {% endif %}

    {% if paginate %}

        <div class="pagination">
            {% if paginate.prevUrl %}
                <span>
                    <a href="{{ paginate.prevUrl }}" class="back">Previous</a>
                </span>
            {% endif %}
            <ul>
                {% for page, url in paginate.getPrevUrls(5) %}
                    <li>
                        <a href="{{ url }}">{{ page }}</a>
                    </li>
                {% endfor %}
                <li>
                    <a href="" class="active">
                        {{ paginate.currentPage }}
                    </a>
                </li>
                {% for page, url in paginate.getNextUrls(5) %}
                    <li>
                        <a href="{{ url }}">{{ page }}</a>
                    </li>
                {% endfor %}
            </ul>
            {% if paginate.nextUrl %}
                <span>
                    <a href="{{ paginate.nextUrl }}" class="forward">Next</a>
                </span>
            {% endif %}
        </div>

    {% endif %}

{% endpaginate %}
| improve this answer | |
3

Well, would this be what you are looking for? - https://github.com/iainurquhart/FacetedNav_CraftPlugin

| improve this answer | |
  • Might possibly work! Will give it a bash tomorrow. – Jason Mayo Jun 24 '15 at 19:16
  • This worked, but I couldn't get it to work as a form. I managed to dig a bit deeper in to stack exchange and found a few related posts and learnt from them to create some code that worked well. Will post shortly. – Jason Mayo Jun 26 '15 at 14:05
  • @JasonMayo can you please share related to search form as i stuck there. – inrsaurabh Apr 17 '19 at 11:24
  • @inrsaurabh See that accepted answer (Mine). I think that's what your asking for? – Jason Mayo Apr 26 '19 at 10:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.