1

I'm trying to build a category filter on a page that lists houses that are for sale. The idea of the filter is to make it possible to select houses which, for example, are for sale or rent, have a certain amount of rooms, a minimum/maximum price and eventually a search option to search at location, postal code or street address.

I first built one filter, which filters if a house is for sale or for rent. This filter works with two anchor buttons in a list. I now want to add the next filter, which is the amount of rooms. This filter should be a dropdown (a select tag with options in it).

I got both filters to work, but I want them to stack upon each other. So when the houses that are for sale are being shown and the user clicks on houses with 4+ rooms, it shows the houses that are for sale AND have 4+ rooms.

What I came up with so far

The filter for sale/rent houses

<ul class="button-group round">
 {% for offerCategory in craft.categories.group('offerTypes').find() %}
  <li>
   <a class="mart-button" href="{{craft.request.url}}/?category={{ offerCategory.slug }}">{{ offerCategory.title }}</a>
  </li>
 {% endfor %}
</ul>

The filter for the amount of rooms

<select onChange="window.location.href=this.value">
 {% for offerRooms in craft.categories.group('offerRoomTypes').find() %}
  <option value="{{craft.request.url}}/?category={{ offerRooms.slug }}" name>{{ offerRooms.title }}</option>
 {% endfor %}
</select>

And the rest

{% if craft.request.getParam('category') is not null %}

 {% set offerCategory = craft.categories.slug(craft.request.getParam('category')) %}
 {% set offerRooms = craft.categories.slug(craft.request.getParam('category')) %}

 {% set offers = craft.entries({
    section: 'offers',
    relatedTo: [
        'and',
        { targetElement: offerCategory },
        { targetElement: offerRooms }
    ]
 }) %}

{% else %}

 {% set offers = craft.entries.section('offers').find() %}

{% endif %}

The URL that's being fetched is now: .../?category=forsale and when selecting rooms .../?category=4rooms. What I want to achieve is something like .../?category=forsale,4rooms (I'm nog sure if that's the right way). If someone also can tell me directly how the search should be implemented that's welcome as well ;). Thanks in advance!

3

Passing the category as a comma delineated string and using craft.request.getParam('category') the second time will not grab the second variable 4rooms. First you need to parse that string into an array. This will create an array ['forsale','4rooms']. Use the parts of that array to assign to your other variables.

{% set params = craft.request.getParam('category')|split(',') %}
{% set offerCategory = craft.categories.slug(params[0]) %}
{% set offerRooms = craft.categories.slug(params[1]) %}
  • Hey aran, thanks for taking time for this. At the time I decided to filter them with jQuery instead, since I won't be getting a lot of entries. I will mark your answer as correct because I think it would as you say. – sanderfish Oct 27 '15 at 7:27

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.