/** * Alter a search query before it gets executed. * * The hook is invoked after all enabled processors have preprocessed the query. * * @param \Drupal\search_api\Query\QueryInterface $query * The query that will be executed. */ function hook_search_api_query_alter(\Drupal\search_api\Query\QueryInterface &$query) { // Do not run for queries with a certain tag. if ($query->hasTag('example_tag')) { return; } // Otherwise, exclude the node with ID 10 from the search results. $fields = $query->getIndex()->getFields(); foreach ($query->getIndex()->getDatasources() as $datasource_id => $datasource) { if ($datasource->getEntityTypeId() == 'node') { if (isset($fields['nid'])) { $query->addCondition('nid', 10, '<>'); } } } }