Remove Certain Content Types From Search Results

Public

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_query_alter().
  3.  * removes content types defined in the excluded_content_types array from search results
  4.  */
  5. function hook_query_alter(&$query) {
  6. $is_search = FALSE;
  7. $excluded_content_types = array(
  8. 'product',
  9. 'billboard',
  10. );
  11. foreach ($query->getTables() as $table) {
  12. if ($table['table'] == 'search_index') {
  13. $is_search = TRUE;
  14. }
  15. }
  16. if ($is_search) {
  17. foreach ($excluded_content_types as $content_type) {
  18. $query->condition('n.type', $content_type, '<>');
  19. }
  20. }
  21. }