Remove node from search

Published by burgerboydaddy on Fri, 10/05/2012 - 19:15


Quickly one.
Today I was tasked to remove node from search results on our side.
It looks like easy but it is not if you are using basic Drupal 7 search engine.
Options are:

  1. remove items from search tables (but they will be re-populated with next cron run)
  2. build module with query alter hook (to much work for simple thing like this)
  3. alter search-result.tpl.php template.

And 3rd one is one that I used. Since site already have inside custom templates folder search-result.tpl.php
I just added at the top:
if ($result["node"]->nid == <excluded nid>) {
      // search result alter
else {
    // everything else

and it is done.
Note to myself: next time use Search API and assign views to the search. Then I  use Views to override search results (including all its features: filtering, sorting, etc).
Create a custom field such as "Exclude this node from search," and set the filter to exclude nodes with that field or/and certain content types.

Add new comment