TransWikia.com

Craft search only shows results matching 'title' of entries

Craft CMS Asked by Sebastián. on May 3, 2021

I´m doing a search on Blog entries, i want to get all relevant entries searching on several fields, and seems that only matches the title. I try already using search(query), now i added the fields, same result. Maybe the search item should be re-generated? i don´t know how to do it in console, but i have only 4 blog entries at the time, i already change and saved all of them, same result…

Any help is welcome. Thanks.

{% paginate craft.entries({
             search: 'title:' ~ query ~ ' OR categoriaBlog:' ~ query ~ ' OR etiquetasBlog:' ~ query ~ ' OR resumenBlog:' ~ query ~ ' OR subtituloBlog:' ~ query ~ ' OR contenidoBlog:' ~ query,
             orderBy: 'score',
             section: 'blogSec'
        })
        .limit(6) as pageInfo, blogs %}

One Answer

Search via search() can be used only for text fields, if you want to search by categories or records you need to use the relatedTo() method, also do not forget to specify search keywords when creating the field.

enter image description here

Links: https://craftcms.com/docs/3.x/relations.html#terminology, https://craftcms.com/docs/3.x/searching.html#supported-syntaxes.

Practical example:

{% set searchResults = [] %}
  
{% set queryParamsText = queryParamsText|merge({
        search: {
            query: 'title:' ~ searchWord
        }
     }) 
%}

    {% set category = craft.categories.group('category').search(searchWord).one() %}

    {% set relatedParams = relatedParams|merge([{ targetElement:category }]) %}

    {% if relatedParams|length > 1 %}
        {% set queryParamsRelated = queryParamsRelated|merge({'relatedTo':relatedParams}) %}
    {% endif %}

{% set searchResultsText = craft.entries(queryParamsText).section('canAddSectionHandleOrId') %}

{% set searchResultsRelated = craft.entries(queryParamsRelated
).section('canAddSectionHandleOrId') %}

{% set searchResults = searchResultsText|merge(searchResultsRelated)|unique %}

Answered by Pavel Pavlovich on May 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP