'Drupal views: bring nodes with taxonomy term X to top

I have a simple view of fields (drupal 9) that lists nodes from a single content type (client). A node has a taxonomy reference field (field_client_country) where an editor can add the country or countries the client is from.

I need to extend the view so that nodes with, for example 'Italy', are brought to the top of the listing. After that, all the other nodes can be displayed.

How is this possible in views? I think i need to be in views_query_alter hook, but whatever i try it doesn't work.

function module_sticky_clients_views_query_alter(&$view, &$query)
{

    $id = $view->id();
    $country_ids = ['312', '112']; //IT-italy, then NL-netherlands

    if ($id == 'view_clients') {

        //bring nodes with $country_ids to top of listing, then show the rest

    }

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source