I have an node view with an exposed filer for nid. It seems that I can put only one value (one nid) in there. But is there anyway I can filter down nodes by multiple nid? I tried these in the form "1 and 2" "1,2" "1 2" but didn't work.


I don't think this can be done by Views only.

You must create a custom module that use the hook_views_query_alter() (The code is for Drupal 7)

function my_module_views_query_alter(&$view, &$query) {
  // we only want to alter this view, so we check against the machine name
  // and we make sure our exposed filter is set
  if ($view->current_display == 'my_current_display' && $view->exposed_raw_input['nid']) {
    // first we grab the value from our filter and convert it into an array
    // make sure you use the same character you used to delimit the string
    $nids = explode(',',$view->exposed_raw_input['nid']);

    // now we loop over the array of values we created from our exposed filter field
    foreach ($nids as $nid){
      // Be conscience of the ‘where[1]’
      // In the $query object you noticed the conditions array had multiple arrays.
      // Each filter in your view will be placed in a conditions array.
      // It is important to include the empty brackets [] after ['conditions']. This will append the new conditions instead of overwriting them.
      $query->where[1]['conditions'][] = array(
        'field' => 'node.nid', // this is ‘table.field’ from your database. If you used another filter than ‘Content: nid’ this will change. i.e ‘users.uid’
        'value' => $nid,  // looped value from our values array
        'operator' => '!=',  // condition operator. I want to filter out nids, so I am using ‘not equal’

Disclaimer: I run a similar issue and the solution gave to be by the below article. The code and the credits go to Peter Schuelke.

More information can be found here:


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.