There are certain content types such as book pages that I don't want to appear on the search results page. I found example code implementing this on drupal 7 here

 * Implements hook_query_alter().
 * removes content types defined in the excluded_content_types array from search results
function hook_query_alter(&$query) {
  $is_search = FALSE;
  $excluded_content_types = array(
  foreach ($query->getTables() as $table) {
    if ($table['table'] == 'search_index') {
      $is_search = TRUE;
  if ($is_search) {
    foreach ($excluded_content_types as $content_type) {
      $query->condition('n.type', $content_type, '<>');

However I want to implement this in 8. Is there a way to exclude certain content types from search results?

UPDATE: I've also attempted to restrict the search types from within the search-result.html.twig however it does not seem like I have the content type variable available.

{{ attach_library('classy/search-results') }}
{{ title_prefix }}

{{ dump(info_split) }}

<h3{{ title_attributes.addClass('search-result__title') }}>
  <a href="{{ url }}">{{ title }}</a>
{{ title_suffix }}
<div class="search-result__snippet-info">
  {% if snippet %}
    <p{{ content_attributes.addClass('search-result__snippet') }}>{{ snippet }}</p>
  {% endif %}
  {% if info %}
    <p class="search-result__info">{{ info }}</p>
  {% endif %}

When performing {{ dump(info_split) }} it does not show content type as an available variable.


Configuring search is easy with Search API. Excluding node types from indexing is one of its basic config options.


When you set up your index and go to the configuration page, you can exclude bundles (types).

|improve this answer|||||
  • I'd rather intercept it and remove it in a hook or template instead. I'd like to avoid installing new modules, especially since I only plan on removing one content type, and there would be less than 10 nodes. Is that a possibility? – Matt Sep 23 '16 at 18:28

I had a similar issue and i managed it by writing


and by enabling advanced search for anonymous users. Also hide the options from users in

YOUR_THEME_NAME_form_search_form_alter(&$form, FormStateInterface $form_state, $form_id)

use these functions and kint($variables) and kint($form). Hope it will help

|improve this answer|||||
  • Where should I write these codes??? – Sky Apr 16 '18 at 18:02
  • In your YOUR_THEME_NAME.theme file – Razeem Ahmad Apr 17 '18 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.