How can I change the search settings so that it only indexes one content type?

Comments

mike.hobo’s picture

This is my code for searching just in the type "blog" you can change it accordingly by just replacing 3 things.

<form action="search/node" method="post" id="search-form" class="search-form">
<div class="form-item">
<input type="text" class="input-text" value="" size="20" name="keys" />
<input type="submit" value="Search" name="op" title="Search" alt="Search" />
<input type="hidden" value="<?php print drupal_get_token('search_form'); ?>" name="form_token" />
<input type="hidden" value="search_form" id="edit-search-form" name="form_id" /> 
<input type="hidden" name="type[blog]" id="edit-type-blog" value="blog" />
</div>
</form>
Delta Bridges’s picture

Hello there,
would this code work with D6?
Many thanks,

drupalftw’s picture

I needed this to work in D6. I just used this in hook_form_alter():

<?php
function MY_MODULE_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
     // validate the search forms and set up the types we can search on.
    case 'search_theme_form':
      $form['#validate'][] = 'MY_MODULE_search_form_validate';
      break;
    case 'search_form':
      $types = 'type:story,blog';
      // If present get rid of string 'type:story,blog'
      if (substr($form['basic']['inline']['keys']['#default_value'], -15, 15) == $types) {
        $form['basic']['inline']['keys']['#default_value'] = substr($form['basic']['inline']['keys']['#default_value'], 0, -16);
      }
      $form['#validate'][] = 'MY_MODULE_search_form_validate';
      break;
  }
}

/**
 * Implementation of hook_validate()
 *
 * Only search on content types: story, blog.
 * 
 * @param array $form
 * @param array $form_state
 */
function MY_MODULE_search_form_validate($form, &$form_state) {
  if ($form_state['values']['form_id'] == 'search_theme_form') {
    // search block form
    $form_state['values']['search_theme_form'] .= ' type:story,blog';
  } else {
    // search page form 
    $form_state['values']['keys'] .= ' type:story,blog'; 
    $form_state['values']['processed_keys'] .= ' type:story,blog';
  } 
}
?>

Think it should work.