I assume you're using .search()
– a common mistake is to try to combine orderBy('score')
with custom fields, which doesn't work because the score
attribute isn't an actual field, but a computed value that Craft "injects" into the element model.
If I understand you correctly, you want to render any "featured" entries at the top of the search results. You could achieve this by doing two separate search queries, before merging the results, with the featured entries at the beginning of the entry results array. Assuming featured
is a Lightswitch field, something like this should work:
{% set searchQuery = craft.entries.search('foo').orderBy('score') %}
{% set featuredEntries = searchQuery.featured('1').all() %}
{% set notFeaturedEntries = searchQuery.featured('not 1').all() %}
{% set allEntries = featuredEntries|merge(notFeaturedEntries) %}
{% for entry in allEntries %}
...
{% endfor %}
If you need to use pagination, the above won't fly – in which case you could opt to just collect the entry IDs and do a third query to pull the full entry models for a paginated result set:
{% set featuredEntryIds = searchQuery.featured('1').ids() %}
{% set notFeaturedEntryIds = searchQuery.featured('not 1').ids() %}
{% set allEntryIds = featuredEntryIds|merge(notFeaturedEntryIds) %}
{% paginate searchQuery.featured(null).limit(10).id(allEntryIds).fixedOrder(true) as pageInfo, pageEntries %}
An alternative approach to the above is to just have a single query, and use the group
filter to sort out the featured entries from the non-featured ones:
{% set entries = craft.entries.search('foo').orderBy('score').all()|group('featured') %}
{% set featuredEntries = entries['1'] ?? [] %}
{% set notFeaturedEntries = entries[''] ?? [] %}
Note: If you're using Craft 2, be sure to replace the orderBy
parameter with order
. Using order
in Craft 3 will still work, but the parameter is deprecated and will not work in Craft 4.
score
andfeatured
are exactly? Custom fields? If so, what type(s)?