I’m trying to set up a filtering system similar to the one found on the product pages on
asos.com. That system has different category groups of filters,
where selecting multiple filters inside a particular category group
widens the results list (OR/conjuction), whereas filters across
category groups narrow the results list (AND/disjunction).
I’m trying to replicate this behaviour in Craft, but I’m
struggling to combine AND and OR relations. I currently do the
following to build up a list of filters across category groups from
a comma separated query string in the form
?regions=22,33&teams=65§ors=42,43,44
:
{% set categoryGroups = ['regions', 'teams', 'sectors', 'topics'] %}
{% set relations = ['and'] %}
{% for group in categoryGroups %}
{% set filterValueString = craft.request.getParam(group) %}
{% if filterValueString != '' %}
{% set filterValues = filterValueString|split(',') %}
{% set relations = relations|merge(filterValues) %}
{% endif %}
{% endfor %}
{% if relations | length > 1 %}
{% set params = params | merge({ relatedTo: relations }) %}
{% endif %}
{% set results = craft.entries(params) %}
この結果、 ['and'、22、33、42、43、44、65]
のようなリレーション
配列が生成され、すでにフィルタリングされたカテゴリグループは、新しいフィルタを包含するように拡大するのではなく、結果セットをさらに制限します。私はおそらく
['と'、[22,33]、[42,43,44]、65]
のように見えるように
relations
配列(カテゴリグループ)は、( relatedTo
に要素IDの標準配列を渡すときと同じように)ORに設定され、外側の関係でANDが提供されますが、これは動作しません。
とにかく、私が探している行動を得るためにはありますか?
私はそれをさらに見て、解決策に着きました。
['and'、{element:[22、33]}、{element:[42,43,48]}、{element:65}の形式にする必要があった
relations }] 。
それは次のようにして行うことができます:
{% set relations = relations|merge([{element:filterValues}]) %}