Questions? Feedback? powered by Olark live chat software Programmatically set a Views filter in Drupal 6 and Views 2 | Zyxware Technologies

Programmatically set a Views filter in Drupal 6 and Views 2

October 14, 2009 - 05:41

Views is one of the most complex of all Drupal modules and one of the most useful of all Drupal modules. The only downside is that, since it is very complex, not too many people have been able to contribute to the documentation of the module. If you search Google for finding out how to programmatically set a filter in Views2 you probably wouldn't find any help. Here is how you do it.

$view = views_get_view('name_of_view');
$filter = $view->get_item($display_id, 'filter', 'name_of_filter');
$filter['value']['value'] = $filter_value;
$view->set_item($display_id, 'filter', 'name_of_filter', $filter);

Yes, that is it. As simple as that.

Still not able to solve your problem? Contact Us for Drupal support.

Get Drupal updates straight to your inbox

To prevent automated spam submissions leave this field empty.

Post your comments / questions


I´m trying to use the code of this post but something goes wrong, my code is:

1. $display_id='page_1'; //id of view
2. global $user;
3. $view2 = views_get_view('my_view_name');
4. $view2->set_display($display_id);
5. $view2->get_total_rows = true;
6. $view2->set_arguments(array($user->uid));
7. $view2->pre_execute();
8. $view2->execute();

when the line 8 is executed it returns to line 1 :O and finally it shows the following error:

Fatal error: Maximum function nesting level of '100' reached, aborting!

Do you know what is the problem?

Thanks a lot.

Where did you put the above code? Somewhere inside views? Or outside in a module? Looks like you had put that right inside the view.

I try to filter a view in my template using your great snippet ;), but I'm not sure how to configure my view in the Drupal interface.
I want to filter my node by taxonomy (term ID).
Question: must I add the filter I want to use in my view configuration (in the filters block) and can I directly add it on my code?

Here is my code:
$view = views_get_view('push_formation');
$display_id = 'default';

$filter = $view->get_item($display_id, 'filter', 'term_node_tid_depth');
$item['value'] = array('value'=>'12'); //Maybe this line is wrong??
$view->set_item($display_id, 'filter', 'term_node_tid_depth', $filter);
$viewsoutput = $view->render();
print $viewsoutput;

Thanks for your help

Yes, you have to create your filter manually via the views interface if you have to be able to modify it programmatically the way is shown in the page. Also try the get_item part and dpm($filter) to see the data structure..

I am trying to use filters to sort an embedded view in a node template:

$view_all = views_get_view('verb_practice_all');
$myview_args_all = array(0 => $tense_name, 1 => $type_name);
$item = $view_all->get_item('default', 'sort', 'nid');
$view_all->set_item('default', 'sort', 'nid', $item);

This works on a view display page when I change line 5 from
$item = $view_all->...
$item = $view->...

It does not work on in the node template.

Any suggestions would be very welcome.
Thanks for posting this here!


You may also like