combine with seoMatic All

 avatar
tommydalton
twig
23 days ago
1.7 kB
3
Indexable
Never
To achieve this, you can first perform a Craft CMS search to retrieve entries matching the query, then search for matching entries in the SEOmatic fields and entry slugs. Here's how you can do it:

twig
Copy code
{% set props = {
    query: craft.app.request.getParam('query') ?? '',
} %}

{# Perform Craft CMS search #}
{% set craftEntries = craft.entries()
    .section('pages')
    .search(props.query)
    .type('not playground')
    .limit(data.pageLinksLimit)
    .all()
%}

{# Initialize an empty array to store unique entries #}
{% set uniqueEntries = [] %}

{# Loop through Craft CMS search results #}
{% for entry in craftEntries %}
    {# Add entry to uniqueEntries array if not already present #}
    {% if entry not in uniqueEntries %}
        {% set uniqueEntries = uniqueEntries|merge([entry]) %}
    {% endif %}
{% endfor %}

{# Perform SEOmatic search #}
{% set seoEntries = craft.entries()
    .section('pages')
    .seoTitle('*' ~ props.query ~ '*')
    .orWhere(['seoDescription:*' ~ props.query ~ '*', 'slug:*' ~ props.query|replace({'-':''}) ~ '*'])
    .type('not playground')
    .limit(data.pageLinksLimit)
    .all()
%}

{# Loop through SEOmatic search results #}
{% for entry in seoEntries %}
    {# Add entry to uniqueEntries array if not already present #}
    {% if entry not in uniqueEntries %}
        {% set uniqueEntries = uniqueEntries|merge([entry]) %}
    {% endif %}
{% endfor %}

{# Output uniqueEntries #}
{% for entry in uniqueEntries %}
    {# Render entry details here #}
    <h2>{{ entry.title }}</h2>
    <p>{{ entry.seoTitle }}</p>
    <p>{{ entry.seoDescription }}</p>
    <p>{{ entry.slug }}</p>
{% endfor %}
Leave a Comment