I have a set of pages that use path information to - amongst other things - create view of relevent news articles.



I have an existing view that uses a contextual filter to extract the relevant portion of the URL and then look up relevant news articles that are tagged with an organisation tag that maps to each service.

This works great, but I have been asked to change the view to the following behavior:

  1. Show ALL recent news articles
  2. Sort the news articles so the relevant (and recent) organisation tagged news is at the top

I am not allowed to radically redesign the page or workflow for staff - eg manually tag all news articles - so the only place I can get the taxonomy term name for the sort is the url.

How can I sort view results based on a taxonomy term name that takes a parameter from the page url?

  • With a little bit of redesign though (e.g. a second pager) it could be as easy as adding another block or views attachment in the bottom of the first view. Nice question!! Looking forward to nice answers.
    – leymannx
    Commented Nov 7, 2022 at 10:18
  • Maybe check out the comment under this answer: drupal.stackexchange.com/a/212340/15055.
    – leymannx
    Commented Nov 7, 2022 at 10:23

1 Answer 1


To accomplish this, you need to alter the views query with hook_views_query_alter in your custom module. Read out the requested URL and get the related taxonomy term to it. Then you can adjust the query according to your needs. A similar answer can be found here: How to sort on different columns in the same view?

Here is an example:

use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;

function MYMODULE_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  // Determine view by id and current display.
  if ($view->id() == 'content_recent' && $view->current_display == 'default') {
    // Get the requested path.
    $request = \Drupal::request();
    $path = $request->getPathInfo();

    // Use some logic to get the term for the current path.
    // @see https://drupal.stackexchange.com/a/266084/15055
    $tid = FALSE;
    if ($path == 'example') {
      $tid = 9;

    // If the URL belongs to a term, we change the order.
    if ($tid) {
      // You may need to add a relationship to the taxonomy terms,
      // if your view is not already doing this or add it in the Views UI.

      $sql = "CASE
        WHEN taxonomy_term_field_data_node_field_data.tid = $tid THEN 0
        ELSE 1
      // Add the sql formula for sorting.
      $query->addField(NULL, $sql, 'related_taxonomy_term');
      // We first sort by the related term and than by the views settings,
      // which should for example be the changed date descending.
      $query->orderby = array_merge([[
        'field' => 'related_taxonomy_term',
        'direction' => 'ASC',
      ]], $query->orderby);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.