<p>You can add custom properties to elements <a href="https://www.yiiframework.com/doc/guide/2.0/en/concept-behaviors" rel="nofollow noreferrer">using behaviors</a>. In this case you want to add your 'getSearchScore()' logic.</p>

<p>Here's <a href="https://nystudio107.com/blog/extending-craft-cms-with-validation-rules-and-behaviors" rel="nofollow noreferrer">a good article on how behaviors work</a> in Craft CMS and <a href="https://github.com/craftcms/docs/pull/92/files?short_path=44ef191#diff-44ef191d813e1c077a869a180265c238" rel="nofollow noreferrer">a current PR on the documentation</a> to have behaviors information added.</p>

CC BY-SA 4.0
2020-05-15T18:07:04.220Z
Thanks! I've been able to now add the behaviour, I am struggling to order by that behaviour when returning the entries
mikestreety
CC BY-SA 4.0
2020-05-14T09:26:50.847Z
2020-05-14T09:26:50.847Z
billythekid
<p><em>TL:DR; How do I add a custom sorting to Craft?</em></p>

<p>I am currently building a system which is very tag-centric. I am rebuilding my side project in CraftCMS (<a href="https://email.subscribeto.at/" rel="nofollow noreferrer">https://email.subscribeto.at/</a>) which was custom PHP as a prototype.</p>

<p>Entries are tagged with one or more categories, then on the front-end the user can search for categories and the results are returned.</p>

<p>Currently, the results can be sorted by "relevance" which I would like to replicate on CraftCMS.</p>

<p>I tried to use <code>.search()</code> as it has a <code>score</code>, however this score is not what I want as it is not additive. I want to search with <code>OR</code> but for the score to be additive.</p>

<p>For example, if I have 2 entries, one with tags "a" and on with "a, b" and I search for "a, b", the first one has a score of 50 (as 100% of it's tags match 50% of the search) and the second a score of 17 (not sure how it gets this, would expect 100%)</p>

<p>Also, <code>.search</code> seems a bit fragile when I could us <code>relatedTo()</code>.</p>

<p>I have a local module installed for some overrides already, so my question is:</p>

<p><strong>How would I add a custom property to each entry and then order by that?</strong></p>

<p>Thanks in advance for your help</p>

<craft3>
Get entries related to tags with a score & order by
# Get entries related to tags with a score & order by

(Asked by: username_0 on 2020-05-11)

*TL:DR; How do I add a custom sorting to Craft?*

I am currently building a system which is very tag-centric. I am rebuilding my side project in CraftCMS (<https://email.subscribeto.at/>) which was custom PHP as a prototype.

Entries are tagged with one or more categories, then on the front-end the user can search for categories and the results are returned.

Currently, the results can be sorted by "relevance" which I would like to replicate on CraftCMS.

I tried to use `.search()` as it has a `score`, however this score is not what I want as it is not additive. I want to search with `OR` but for the score to be additive.

For example, if I have 2 entries, one with tags "a" and on with "a, b" and I search for "a, b", the first one has a score of 50 (as 100% of it's tags match 50% of the search) and the second a score of 17 (not sure how it gets this, would expect 100%)

Also, `.search` seems a bit fragile when I could us `relatedTo()`.

I have a local module installed for some overrides already, so my question is:

**How would I add a custom property to each entry and then order by that?**

Thanks in advance for your help

**Tags:** craft3


---

## 1 answers

### Answer by username_1 on 2020-05-14. Score: 3

You can add custom properties to elements [using behaviors](https://www.yiiframework.com/doc/guide/2.0/en/concept-behaviors). In this case you want to add your 'getSearchScore()' logic.

Here's [a good article on how behaviors work](https://nystudio107.com/blog/extending-craft-cms-with-validation-rules-and-behaviors) in Craft CMS and [a current PR on the documentation](https://github.com/craftcms/docs/pull/92/files?short_path=44ef191#diff-44ef191d813e1c077a869a180265c238) to have behaviors information added.

> **Comments:**
> **username_0**: Thanks! I've been able to now add the behaviour, I am struggling to order by that behaviour when returning the entries  

---

