<p>I assume you're using <code>.search()</code> – a common mistake is to try to combine <a href="https://docs.craftcms.com/v3/searching.html#ordering-results-by-score" rel="noreferrer"><code>orderBy('score')</code></a> with custom fields, which doesn't work because the <code>score</code> attribute isn't an actual field, but a computed value that Craft "injects" into the element model.</p>

<p>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 <code>featured</code> is a Lightswitch field, something like this should work:</p>

<pre><code>{% 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 %}
</code></pre>

<p>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:</p>

<pre><code>{% 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 %}
</code></pre>

<p>An alternative approach to the above is to just have a single query, and use the <code>group</code> filter to sort out the featured entries from the non-featured ones:</p>

<pre><code>{% set entries = craft.entries.search('foo').orderBy('score').all()|group('featured') %}
{% set featuredEntries = entries['1'] ?? [] %}
{% set notFeaturedEntries = entries[''] ?? [] %}
</code></pre>

<p><strong>Note:</strong> If you're using Craft 2, be sure to replace the <code>orderBy</code> parameter with <code>order</code>. Using <code>order</code> in Craft 3 will still work, but the parameter is deprecated and will <em>not</em> work in Craft 4.</p>

CC BY-SA 4.0
2020-11-10T22:40:45.337Z
This worked great for pagination and placing 'featured' entries at the top. I did have to edit the `paginate` tag from `searchQuery` to `craft.entries()` to get the correct order.
Jeff Irwin
CC BY-SA 4.0
2022-11-06T02:01:13.780Z
OMG, tried everything to get a custom pagination to work, finally got it with `.fixedOrder(true)` thanks!
Leo Leoncio
CC BY-SA 4.0
2018-07-27T19:59:05.980Z
2018-07-27T19:59:05.980Z
Mats Mikkel Rummelhoff
<p>Currently I am sorting a category template with this:</p>

<p>'featured desc, postDate desc'</p>

<p>Works as expected. But I would like to sort it first on the featured field, then on the score. Such as</p>

<p>'featured desc, score'</p>

<p>But this throws an error. Can someone explain why that is and how I might achieve what I'm looking for? The first search is good for bringing featured posts to the top, but if there are none or maybe one, the rest of the results are not all that relevant. Ideas?</p>

<p>Thanks in advance.</p>

CC BY-SA 4.0
2018-07-27T19:21:07.320Z
It would be great if you could post your whole query and also clarify what `score` and `featured` are exactly? Custom fields? If so, what type(s)?
Jalen Davenport
<entries><categories><sort>
Order by score and another field
# Order by score and another field

(Asked by: username_0 on 2018-07-27)

Currently I am sorting a category template with this:

'featured desc, postDate desc'

Works as expected. But I would like to sort it first on the featured field, then on the score. Such as

'featured desc, score'

But this throws an error. Can someone explain why that is and how I might achieve what I'm looking for? The first search is good for bringing featured posts to the top, but if there are none or maybe one, the rest of the results are not all that relevant. Ideas?

Thanks in advance.

**Tags:** entries, categories, sort
> **Comments:**
> **username_1**: It would be great if you could post your whole query and also clarify what `score` and `featured` are exactly? Custom fields? If so, what type(s)?  

---

## 1 answers

### Answer by username_2 on 2018-07-27. Score: 10 | Accepted answer

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.

> **Comments:**
> **username_3**: This worked great for pagination and placing 'featured' entries at the top. I did have to edit the `paginate` tag from `searchQuery` to `craft.entries()` to get the correct order.  
> **username_4**: OMG, tried everything to get a custom pagination to work, finally got it with `.fixedOrder(true)` thanks!  

---

