1

***I have just realised I am mixing up my filtering and sorting. I have amended the question to try and make it a bit clearer.

I'm trying to use SuperSort plugin to run different filters, but I'm struggling as only the last filter gets applied. I would like to add the filter to a button or link, so that when the user clicks on it, it activates the filter. Any tips?

<h1>search</h1>

<form action="{{ url('search') }}">
    <input type="search" name="q" placeholder="Search">
    <input type="submit" value="Go">
</form>

{% set query = craft.request.getParam('q') %}
{% set users = craft.users.search(query).order('dateUpdated')|reverse %}

{% if users|length %}
    <p></p>
    <ul>
        <span<a href="{{ users.dateUpdated }}">{{ users|length }} results:</a></span>
        <li><a href="{% set usersByFirstName = craft.users.search('sortAS', '{{ firstName}}') %}">A-Z</a></li>

        <li><a href="{% set users = craft.users.search(query).order('firstName')|reverse%}">Z-A</a></li>

        <li><a href="#">Nearby</a></li>
        <li><a href="#">Newest</a></li>
        <li><a href="#">Oldest</a></li>
    </ul>
{% endif %}

For example:

link#1 - sort A-Z link#2 - sort dateUpdated

| improve this question | | | | |
  • Can you clarify the question a bit more? I am not sure what information you are looking for. Thanks! – Alan Miller Aug 29 '17 at 4:06
  • The filters are currently overwriting each other, so only the last filter applies. I'd like to use different filters like this: link#1 - filter A-Z link#2 - filter dateUpdated Hope that makes more sense – Remco Aug 29 '17 at 9:35
4

So in order to do this you have to reformat your code and your strategy a bit. You cant provide a Craft query as a link, all that does is grab an array from the database and it won't inherently print something out.

<li><a href="{% set usersByFirstName = craft.users.search('sortAS', '{{ firstName}}') %}">A-Z</a></li>

The best way to change the way your results are being sorted (if you want to do it via Twig and not something front-end like JS is to send the sort style through a query string.

So let's assume you are returning a list of users and you want to sort them by first name alpha ascending:

{% set users = craft.users.order('firstName asc').find() %}

If you want to change the direction of the order create a link like this:

<a href="url/user-list?sort=desc">Sort Z-A</a>

When the page reloads a new query parameter is available to you sort, you can get it with this code:

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

Now you can use that variable to alter the way your original query works. Something like this... starting over from scratch:

{% if craft.request.getParam('sort')|length > 0 %}

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

 {% else %}

    {% set sort = 'asc' %} {# default sort #}

 {% endif %}

Then in the Craft query, you could just user this instead of our original query above:

 {% set users = craft.users.order('firstName ' ~ sort).find() %}

Now when the page first loads you will get users sorted by firstName asc and when you click the filter it will reload with firstName desc.

You can apply this same query string method to all of the parameters. Getting variables like:

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

Where sortBy equals firstName or lastName

That query string would look like:

url/user-list?sort=desc&sortby=firstName

And so on.

| improve this answer | | | | |

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.