1

I've a view with sorting, that I need to call programmatically like,

$view_name = 'view_name';
$display_id = 'page';
$exposed_input = Array
(
    ['field_year_value'] => 
    ['shs_term_node_tid_depth'] => All
    ['field_federal_entity_value'] => All
    ['order'] => title
    ['sort'] => desc
);

$exposed_filters = Array
(
    ['field_year_value'] => 
    ['shs_term_node_tid_depth'] => All
    ['field_federal_entity_value'] => All
)

$view = views_get_view($view_name);
$view->exposed_input = $exposed_input;
$view->exposed_raw_input = $exposed_filters;
$view->set_display($display_id);
$view->pre_execute();
$view->execute();

But the result is not sorted.

Is there a way to accomplish this?

0

You could check this sample code. It contains additional methods in Views that you could use like in the line:

//Here we can alter the order
//$view->set_item('default', 'sort', 'random', NULL);
if(isset($alter['node_created_order'])){
  $view->add_item('default', 'sort', 'node', 'created',array('order' => $alter['node_created_order']));
}
| improve this answer | |
0

I did it like

    $orderby = Array
(
    [0] => Array
        (
            [field] => node_title
            [direction] => DESC
        )

);
$view->pre_execute();
$view->query->orderby = $orderby;

Note: the orderby will be added after $view->pre_execute();

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy