combine

 avatar
tommydalton
twig
a year ago
1.9 kB
7
Indexable
{% set props = {
    query: craft.app.request.getParam('query') ?? '',
} %}

{# Default Craft search #}
{% set defaultSearchResults = craft.entries()
    .section('pages')
    .search(props.query)
    .type('not playground')
    .limit(null)
    .all() %}

{# Initialize a collection to hold all unique entries #}
{% set allEntries = [] %}
{% set allEntryIds = [] %}

{# Add default search results to the collection #}
{% for entry in defaultSearchResults %}
    {% if entry.id not in allEntryIds %}
        {% set allEntries = allEntries|merge([entry]) %}
        {% set allEntryIds = allEntryIds|merge([entry.id]) %}
    {% endif %}
{% endfor %}

{# Custom search for SEOmatic fields (assuming seoTitle and seoDescription are searchable text fields) #}
{# Adjust according to your actual field handles and ensure they are searchable #}
{% set seomaticEntries = craft.entries()
    .section('pages')
    .type('not playground')
    .limit(null)
    .all() %}

{% for entry in seomaticEntries %}
    {% set seo = seomatic.seoForElement(entry) %}
    {% if props.query in entry.slug or props.query in seo.metaGlobalVars.seoTitle or props.query in seo.metaGlobalVars.seoDescription %}
        {% if entry.id not in allEntryIds %}
            {% set allEntries = allEntries|merge([entry]) %}
            {% set allEntryIds = allEntryIds|merge([entry.id]) %}
        {% endif %}
    {% endif %}
{% endfor %}
Display the Results

twig
Copy code
{% for entry in allEntries %}
    <div class="col px-0 h-100 mb-6"> 
        <h5><a href="{{ entry.url }}">{{ entry.title }}</a></h5>
        {% set seo = seomatic.seoForElement(entry) %}
        {% if seo.metaGlobalVars.parsedValue('seoDescription')|length %}
            <div class="redactor">{{ seo.metaGlobalVars.parsedValue('seoDescription') }}</div>
        {% endif %}
    </div>
{% endfor %}
Editor is loading...
Leave a Comment