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.



Comments
it works for drupal 7 and views 3 also
Anonymous (not verified) wrote on March 21, 2012 - 02:48i can confirm this works for drupal 7 and views 3 also.
also when you have to set filter like "between". you can do something like this:
$filter = $view->get_item($display_id, 'filter', $key);
$filter['value']['min'] = $value['min'];
$filter['value']['max'] = $value['max'];
$view->set_item($display_id, 'filter', $key, $filter);
all the best
I am trying to use filters to
Sarah (not verified) wrote on October 6, 2011 - 03:50I am trying to use filters to sort an embedded view in a node template:
$view_all = views_get_view('verb_practice_all');
$view_all->set_display('default');
$myview_args_all = array(0 => $tense_name, 1 => $type_name);
$view_all->set_arguments($myview_args_all);
$item = $view_all->get_item('default', 'sort', 'nid');
$view_all->set_item('default', 'sort', 'nid', $item);
$view_all->execute_display('default');
This works on a view display page when I change line 5 from
$item = $view_all->...
to
$item = $view->...
It does not work on in the node template.
Any suggestions would be very welcome.
Thanks for posting this here!
How to edit filter in views
Maxime (not verified) wrote on July 4, 2011 - 22:03I 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';
$view->set_display($display_id);
$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
webmaster wrote on July 4, 2011 - 22:37Yes, 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..
Something goes wrong
Ángel C. Lázaro (not verified) wrote on January 5, 2010 - 15:07Hi,
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 code
webmaster wrote on March 1, 2010 - 00:16Where did you put the above code? Somewhere inside views? Or outside in a module? Looks like you had put that right inside the view.