2

In Craft CMS I want to search for the search query value for only some fields/ multiple fields - but not all. For example limiting to the fields title, introduction, cardContent.

I've added a search: property to to my queryEntry object with the value of title and the query string. But I would like to add more fields.

{% set searchQuery = craft.app.request.getParam('q') %}
{# {% set queryEntries = craft.entries({
    section: queryFilters
}).search(searchQuery) %} #}


{% set queryEntries = craft.entries({
        search: 'title:' ~ searchQuery,
        order: 'score'
}) %}
DumbDevGirl42069
  • 891
  • 5
  • 18
  • 47

2 Answers2

0
{% if craft.app.request.getParam('q') %}
    {% set searchQuery = '"' ~ craft.app.request.getParam('q') ~ '"' %}
    {% set queryEntries = craft.entries({
            search: 'title:' ~ searchQuery ~ ' OR cardContent:' ~ searchQuery ~ ' OR introduction:' ~ searchQuery ,
            order: 'score'
}) %}
{% endif %}
  • Get the query string
  • add searchTerms is concatinated in a string using OR and the query
  • This returns the array of entries matching the queryEntries.search and you can do what you like with this - eg loop over and display results
DumbDevGirl42069
  • 891
  • 5
  • 18
  • 47
0

You can concatenate any number of fields with their value in the variable and then you can simple pass that in the search parameter with entries query. Here is the example code for that.

{% set nameparam = craft.app.request.getParam('data') %}
{% set categoryparam = craft.app.request.getParam('data1') %}

{% set queryString = '' %}

{% if nameparam is defined and nameparam is not empty %}
        {% set queryString = queryString ~ 'title:*'~nameparam~'* ' %}        
{% endif %}

{% if categoryparam is defined and categoryparam is not empty %}
    {% set queryString = queryString ~ 'blogCategory:'~categoryparam~' ' %}
{% endif %}

{% if queryString is defined and queryString is not empty %}
    {% set queryParams = {
        search: {
            query: queryString,
            order: 'score'
        },
    } %}
{% else %}
    {% set queryParams = {} %}
{% endif %}

{% set queryEntries = craft.entries(queryParams) %}
Dharman
  • 30,962
  • 25
  • 85
  • 135
ZealousWeb
  • 1,647
  • 1
  • 10
  • 11