1

I've got a site-wide search box (based on the example in the documentation). It's doing a good job of searching through the entries, but is there a way that I can include a search through a user group like authors as well and include it in the same list of results?

Currently I just have this but its not getting any results for author searches even though I know they exist. It seems like the authors should be searchable since they're linked to the entry...

{% set entries = craft.entries.search(query).order('score') %}

If I did another search through craft.users.search I could get another list of results, but I'd like to have one combined list. Is something like that possible?

3

You could use the Twig functions merge and default to merge the two sets of entries.

{% set entries = craft.entries.search(query).order('score')|default([]) %}
{% set authors = craft.users.search('fullName:*' ~ query ~ '*')|default([]) %}

{% if authors %}
    {% set results = entries|merge(authorArticles) %}
{% endif %}

<p>
    You have {{ results|length }} {{ results|length == 1 ? 'result' : 'results' }}.
</p>

The default function is a handy stand-in for short circuit evaluation in twig, so that you can be sure to at least pass an empty array to merge.

|improve this answer|||||

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.