0
votes

i have a view that show all contents of type NEWS NEWS has like fields :

Content = Title

Scheduler = publish-on

and in sort criteria : i want sort contents with two option : With scheduler (publish-on = DESC) and With post date

i want sort content with scheduler, and when scheduler field is empty he sort with post date i have created hook views query alter and i have a problem how i sort field with condition if publishon is empty then sort with postdate

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


    if($view->name === 'articleview') {
        $view->query->add_where(1,'publish_on',  'NOT IN');
        $view->query->orderby[0]['field'] = "CASE WHEN publish_on IS NULL THEN created ELSE publish_on END";
        $view->query->orderby[0]['direction'] = "DESC";
        $view->query->orderby[1]['field'] = "created";
        $view->query->orderby[1]['direction'] = "DESC";
    }
}
1

1 Answers

0
votes

If I understood you right, you should use ternary operation like condition ? value_if_true : value_if_false in place where you want to put value_if_true or value_if_false.