1

I'm trying to filter entries down based on keywords and also if they have selected a letter that matches the first letter of an entry title on Craft 3.

So far I have this:

{% set keywordParam = craft.request.param('keyword') %}
{% set characterParam = craft.request.param('filterByCharacter') %}

{% set entriesParams = {
   section: 'people',
   level: 1
} %}

{% set searchArray = ['OR'] %}

{% if characterParam|length %}
   {% set searchArray = searchArray|merge(['title:' ~ characterParam ~ '*']) %}
{% endif %}

{% if keywordParam|length %}
   {% set searchArray = searchArray|merge(['title:' ~ keywordParam]) %}
{% endif %}

{% if searchArray|length %}
   {% set entriesParams = entriesParams|merge({ search: searchArray }) %}
{% endif %}

{% set peopleEntries = craft.entries(entriesParams) %}

At the moment this is producing the error:

Undefined index: query

I've narrowed it down to this line(s):

{% if searchArray|length %}
   {% set entriesParams = entriesParams|merge({ search: searchArray }) %}
{% endif %}

But I can't figure out what is wrong with this, and what could be going wrong!

An example URL is:

http://localhost:3000/our-people?keyword=sach&filterByCharacter=M

3

Just looked at the docs (https://docs.craftcms.com/v3/searching.html#supported-syntaxes) and it looks like the accepted syntax for this would be:

title::M* OR title:sach

As a result, modifying your code like so should achieve this:

{% set keywordParam = craft.request.param('keyword') %}
{% set characterParam = craft.request.param('filterByCharacter') %}

{% set entriesParams = {
   section: 'people',
   level: 1
} %}

{% set searchArray = [] %}

{% if characterParam|length %}
   {% set searchArray = searchArray|merge(['title::' ~ characterParam ~ '*']) %}
{% endif %}

{% if keywordParam|length %}
   {% set searchArray = searchArray|merge(['title:' ~ keywordParam]) %}
{% endif %}

{% if searchArray|length %}
   {% set searchParam = searchArray|join(' OR ') %}
   {% set entriesParams = entriesParams|merge({ search: searchParam }) %}
{% endif %}

{% set peopleEntries = craft.entries(entriesParams) %}

Hope that resolves it for you.

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.