How to sort view by content type, using literal ordering, as second order-by clause In a view, I want to sort content by:


Collection (a taxonomy term)
Content type
Something else
Something else ...


I have this configured in the View UI and I want to leave most of it as it  is but for the content type ordering, I want to make this literal - I want to state the order exactly ("article", "page", "thingy", "other one").

For this in MySQL you would use FIELD()

I have tried using hook_views_query_alter and setting $query-&gt;orderby[1][field] to FIELD(node_field_data_type, "article", "page", "thingy", "other one") but I am running into exactly this problem: https://www.drupal.org/node/1382016#comment-12058755 In other words, the brackets and quotes are being removed so that it thinks it's one long field name (which of course does not exist) rather than treating it as a function call. It seems this did not happen in Drupal 7.

Is there a known way to get around this in Drupal 8?
Although there may be a method to do this via a query alter I was unable to find the correct solution. I was however able to find a solution.

Although we utilise search API heavily and we can use the content type relevance weighting we had a requirement to sort by a custom content type order and then by date, in addition some content types needed the same weight.

Our solution: Add a search weighting field to the required content types (e.g. integer 1-5) and include it in the index.  In the view add a sort filter for your search filter field.   

You'll need to batch process or use views bulk operations to fill in existing node data and hide the field from content add / edit screens if required.  

One added benefit of this method is, if the client wants to have a piece of content show at the top when it's in the result set they can manually amend the new field for it.