Programmatically set a Views filter in Drupal 6 and Views 2

| | 1 min read

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');
$view->set_display($display_id);
$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.