Exclude whole content types from your Drupal search results

Here's an easy way to entirely exclude all nodes of a certain content 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, and yourmodule with the name of your module:

 * 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);

This example should work for Drupal 5 and 6.

NOTE: This article was first published on The Web Developer's Blog, which has now been discontinued.