Closed AmandaLutzTO closed 7 months ago
Hi @AmandaLutzTO
Thank you for you message. Unfortunately, out of the box it is not possible to search across different element types.
However there is a bit of a work around solution that you could try which would be the following:
{% set query = 't-shirt' %}
{% set elementQuery = create('craft\\elements\\db\\ElementQuery', ['craft\\base\\Element']) %}
{% do elementQuery.search(query) %}
{% do elementQuery.orderBy('score') %}
{% do elementQuery.limit(4) %}
{% set elementIds = elementQuery.ids() %}
{% set entries = craft.entries.id(elementIds).fixedOrder().indexBy('id').all() %}
{% set products = craft.shopifyProducts.id(elementIds).fixedOrder().indexBy('id').all() %}
<ul>
{% for id in elementIds %}
<li>
{% if entries[id] is defined %}
{{ entries[id].title }}
{% elseif products[id] is defined %}
{{ products[id].title }}
{% endif %}
</li>
{% endfor %}
</ul>
Hope this helps, thanks!
This was a good solution. Thanks for your help!
For anyone else who finds this: watch out! This method also returns matching assets, categories, etc to elementQuery. Listing entries and products with the for/if statements was fine but it really messed up my pagination.
I ended up doing a search query for elements, a search query for products, merging them to 1 collection, and then my for loop includes a twig sort with spaceship operator (https://twig.symfony.com/doc/3.x/filters/sort.html). I'm open to modifying my code if anyone has thoughts/suggestions for improvement.
Description
Client wants products to be included in site search.
Steps to reproduce
{% set entries = craft.entries().search(query).orderBy('score').limit(4) %}
Additional info