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(
'product',
'billboard',
);
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>
</h3>
{{ 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 %}
</div>
When performing {{ dump(info_split) }} it does not show content type as an available variable.