Programmatically Render View with Arguments

Public

Drupal Views 2 & 3 Programmatically Render View with Arguments

</> CopyGet raw version
php
  1. $display_id = 'default';
  2. $foo = '';
  3. $view = views_get_view('foobar');
  4. $view->set_display($display_id);
  5. $view->set_arguments(array($foo));
  6.  
  7. //replace name_of_filter with your filter machine name
  8. $view->set_display($display_id);
  9. $filter = $view->get_item($display_id, 'filter', 'name_of_filter');
  10. $filter['value']['value'] = $filter_value;
  11. $view->set_item($display_id, 'filter', 'name_of_filter', $filter);
  12.  
  13. //use this to count the results of your view
  14. $results_count = count($view->result);
  15. //print the rendered version of your view
  16. print $view->render();

Comments

varvashenia's picture

<?php print views_embed_view('blog_posts','block_1', $node->nid); ?>

Victor's picture

$view->set_display($default_id); -> $view->set_display($display_id);