1

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 %}
2

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 %}
3
  • Hi Pavel, thanks for your response. Question: Can´t i search text con category title or tags title? Docs says that i can search on those attributes. And matrix fields with Redactor rich text? – Sebastián. Oct 7 '20 at 17:07
  • I will try your solution and check in deep the links, thank you! – Sebastián. Oct 7 '20 at 17:13
  • @Sebastián. It's my pleasure. In a text search, you will search for your category titles and reach text, Craft CMS will give you the essence where this text appears. – Pavel Pavlovich Oct 7 '20 at 18:12

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.