g4

 avatar
tommydalton
twig
12 days ago
1.8 kB
2
Indexable
Never
{% set props = {
    query: craft.app.request.getParam('query') ?? '',
} %}

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

{# Search in SEOmatic fields #}
{% set seomaticResults = craft.entries()
    .section('pages')
    .type('not playground')
    .all() %}

{% set seomaticFilteredResults = [] %}
{% for entry in seomaticResults %}
    {% set seoTitle = entry.seo.metaGlobalVars.parsedValue('seoTitle') %}
    {% set seoDescription = entry.seo.metaGlobalVars.parsedValue('seoDescription') %}
    {% set modifiedSlug = entry.slug|replace({'-': ''}) %}
    
    {# Check if the query matches any of the SEOmatic fields or modified slug #}
    {% if props.query in seoTitle or props.query in seoDescription or props.query in modifiedSlug %}
        {% set seomaticFilteredResults = seomaticFilteredResults|merge([entry]) %}
    {% endif %}
{% endfor %}

{# Combine and remove duplicates #}
{% set combinedResults = craftResults|merge(seomaticFilteredResults) %}
{% set uniqueResults = [] %}
{% set seenIds = [] %}
{% for entry in combinedResults %}
    {% if entry.id not in seenIds %}
        {% set uniqueResults = uniqueResults|merge([entry]) %}
        {% set seenIds = seenIds|merge([entry.id]) %}
    {% endif %}
{% endfor %}

{# Output the results #}
{% for entry in uniqueResults %}
    <div class="col px-0 h-100 mb-6"> 
        <h5 class=""><a href="{{ entry.url }}">{{ entry.title }}</a></h5>
        {% if entry.seo.metaGlobalVars.parsedValue('seoDescription')|length %}
        <div class="redactor">{{ entry.seo.metaGlobalVars.parsedValue('seoDescription') ?? '' }}</div>
        {% endif %}
    </div>
{% endfor %}
Leave a Comment