Exclude all nodes of a Drupal node type from the search results

31-Jul-2009
Filed under: Drupal

Here's an easy way to entirely exclude all nodes of a certain type from your search results using hook_db_rewrite_sql. Simply create a Drupal module containing the following snippet, and change the $excluded_content_types array to contain the content types you wish to exclude:

<?php
/**
* Implementation of hook_db_rewrite_sql()
*/
function yourmodule_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
 
// Exclude the following content types from the search results
 
$excluded_content_types = array('page', 'story');
 
  if (
$query == '' && $primary_table == 'n' && $primary_field = 'nid' && empty($args)) {
   
$where = " n.type NOT IN ('" . implode(', ', $excluded_content_types) . "') ";
    return array(
'where' => $where);
  }
}
?>