Manually printing a Drupal view with PHP

In order to manually print a view you can run the following code:

views_get_view('my_view_machine_name')

There are some cases where you may want to alter the view before it is transformed into HTML. You can achieve that with the following code.

//Get the view and push it to its pre-execution state
$my_view = views_get_view('my_view_machine_name');
$my_view->init_display();
$my_view->pre_execute();

//Do whatever you want to alter the view object now
//For example, pass arguments to it.
$my_view->args[] = 1;

//Execute to get results filled in
$my_view->execute('diplay_name');

//Then return the view as HTML
$my_view->preview();
Tags: