views query alter not working when view appears in sidebar

by miske   Last Updated May 02, 2015 23:03 PM

I have a block view, and I want to change my query, to add contextual filter. I made a views query alter but when the view appears in sidebar as block view this function doesn't work.

function bpm_views_query_alter(&$view, &$query) {
   if ($view->name == 'single_job' && $view->display_handler->display->id =='block_1' ){
      $nid = _get_job_nid_from_url();
      $query->where[1]['conditions'][2]['value'] = $nid;
   }
}

If I add the same block view in page content then views query alter work as expected. The page is created by page callback and this is following code how i add block view in page content

$output = views_embed_view('single_job', $display_id = 'block_1', $category);


Answers 2


You can get the values from the url with $view->args[0] or $_GET['q'].

Jose Daniel
Jose Daniel
June 24, 2013 04:10 AM

I think you problem is there that $nid not passed correctly,edit your views, instead of your filter, set filter by contextual filter then in your page passed it by contextual filter parameter with

$view= views_embed_view('single_job', $display_id = 'block_1', $category);
$view->set_arguments(array($node->nid));
$output = $view->preview();
print $output;

not longer need query alter function

/*
function bpm_views_query_alter(&$view, &$query) {
  if ($view->name == 'single_job' && $view->display_handler->display->id =='block_1' ){
    $nid = _get_job_nid_from_url();
  $query->where[1]['conditions'][2]['value'] = $nid; 
 }
}*/
zhilevan
zhilevan
November 29, 2014 20:46 PM

Related Questions




How to alter combined filter value programatically?

Updated March 27, 2015 14:18 PM