Craft version: 3.3.19

Is it possible to set default order for CP entry search results? The default is search score but for instance for blog entries, news, etc. the date of the entry is usually more important than the relevance. I need to overwrite the default behaviour somehow.



Maybe? Pretty hacky I think, but there is an EVENT_AFTER_SEARCH you can listen to that has the results already scored.

You could probably take those results, then resort by whatever other attributes you want and pass them along back to the CP.


Thanks Brad but it seems to me 'EVENT_AFTER_SEARCH' is not really suitable for this. I've found a better way on the frontend though in case you or somebody else gonna be interested:

I did some investigation how search and element sorting works and surprisingly the sorting by 'score' attribute is coming from the frontend.

The only thing I had to do is to overwrite the original EntryIndex js class which controls the behaviour of the entry index page. This is definitely possible the following non-hacky way:

Craft.OriginalEntryIndex = Craft.EntryIndex;
Craft.EntryIndex = Craft.BaseElementIndex.extend(
        startSearching: function () {
            this.setSortAttribute('postDate'); // overwrite attr.
            this.setSortDirection('desc'); // overwrite direction

delete (Craft._elementIndexClasses["craft\\\elements\\\Entry"]);
Craft.registerElementIndexClass("craft\\\elements\\\Entry", Craft.EntryIndex);

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.