You are here

function search_exclude_nid_query_alter in Search exclude nid 7

Same name and namespace in other branches
  1. 8 search_exclude_nid.module \search_exclude_nid_query_alter()

Implements hook_query_alter().

File

./search_exclude_nid.module, line 50
Excludes single node pages from search results

Code

function search_exclude_nid_query_alter(QueryAlterableInterface $query) {
  if ($query
    ->hasTag('node_access') && $query
    ->hasTag('pager')) {
    $tables = $query
      ->getTables();
    foreach ($tables as $table) {
      if ($table['table'] == 'search_index') {

        // Filter the excluded nids to make sure we dont break the query.
        $excluded_nids = variable_get('search_exclude_nid_search_exclusion_nids', array());
        if (count($excluded_nids)) {

          // Remove the node IDs from the query.
          $query
            ->condition('n.nid', $excluded_nids, 'NOT IN');
        }
      }
    }
  }
}