You are here

23 January 2013

How to use select query as where clause in Drupal views module query alter. You can't use hook_views_query_alter, because it places single quotes around the value. So you can't use query instead of value. Instead use hook_views_post_build.

Source code viewer
  1. /**
  2.  * Implements hook_views_query_alter().
  3.  *
  4.  * Affects pager and other extras
  5.  */
  6. function HOOK_views_query_alter(&$view, &$query) {
  7. if ($view->name == 'MY_VIEW') {
  8. $query->add_where_expression(1, 'node.nid = :nid', array(':nid' => 1));
  9. }
  10. }
  11.  
  12. // -------------------- OR --------------------
  13.  
  14. /**
  15.  * Implements hook_views_post_build().
  16.  *
  17.  * Example use of select query in where clause
  18.  */
  19. function HOOK_views_post_build(&$view) {
  20. if ($view->name == 'MY_VIEW') {
  21. $view->build_info['query']->where("
  22. node.nid IN (
  23. SELECT nid
  24. FROM my_nid_table
  25. WHERE my_nodes = 0
  26. )
  27. ");
  28. }
  29. }
Programming Language: PHP