0

I have a view listing articles grouped by year (on the post date) which returns me a HTML list like this.

2018

- Article Feb 01, 2018
- Article Jan 05, 2018

2017 

- Article Dec 01, 2017
- Article Nov 05, 2017
- Article Oct 03, 2017
- Article Sep 05, 2017

The years are correctly sorted in descending order, but I would like to have the nodes of each year sorted in ascending order, as in the following list.

2018
- Article Jan 05, 2018
- Article Feb 01, 2018

2017
- Article Jan 10, 2017
- Article Oct 03, 2017
- Article Nov 05, 2017
- Article Dec 01, 2017

I have tried to apply an ascending sort on the content, but it also affects the years.

I have also checked Views: how to ASC sort the grouping field #1 and sort DESC its content?, but I don't want to add an extra field for the year.

I have also tried the solution posted on Views, Group By, can't specify order? but it didn't work for me.

How can I achieve it?

1 Answer 1

0

You can use hook_views_query_alter


function MODULENAME_views_query_alter(&$view, &$query) {
   //print_r($view->query->orderby);
  if ($view->name == 'VIEWNAME') {
  // check the position on the orderBy function in the query
  // For this you can check the values with print_r() function.
   $view->query->orderby[0]['direction'] = "ASC";
  }
}

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.