Easily reorder search results: Revision

The general search page (?search_type=all) returns results grouped by type/subtype, and the code that generates these is not designed to be extensible at all. Thankfully the ability to rewrite view output via hooks makes this a breeze.

Each section of the search results is output by the search/list view. So let's just capture all these view strings into an array, reorder the array, and put it back into the page. We can do this by dropping a unique token during the first call of the search/list view, then filter the page/default view to replace the token with the array.

Note: In the code below I use anonymous functions as hook handlers, and I reorder the list by capturing the content of h2.search-heading-category element. Then a clever algorithm allows us to move keys to the top of an array (PHP could really use native functions to reorder keys). Below I move Pages, Files, and Bookmarks to the top of the results. Your ordering spec must match the HTML: If a heading has an "&" you must use an "amp" entity.