Get paid to write Drupal tutorials on WebWash

How To Create Search Pages Using Views In Drupal 7

Want to learn more about search in Drupal 7? Then check out our online video course called “Build Powerful Search Pages with Views and Search API in Drupal 7“.

A while ago I wrote two tutorials on using the Search API module. In the first tutorial, we created a custom search page using just Views and a Search API index. In the second, we looked at faceted search using the Facet API module.

Search API can be tough to setup especially for new users to Drupal. For example, you need to understand the difference between a Search API Server and Index, and then you need to spend time configuring everything.

In this tutorial, I’ll show you how to create a search page using just Views and Drupal’s core Search module.

Getting Started

Before we can begin, download and enable Views. Also make sure you have enabled the core Search module.

If you use Drush, run the following command:

$ drush dl ctools views
$ drush en views views_ui

Search Module

First, make sure you have the Search module enabled. If you installed Drupal using the standard installation profile, then you’ll have the module already installed.

Second, make sure you create some content and index that content. Go to Configuration -> “Search settings” and click on “Re-index site”.

Fig 1.0

Finally, go to Reports -> “Status report” and click on “run cron manually” in the “Cron maintenance tasks” row.

Fig 1.3

When you click on “Re-index site” button, this doesn’t index any content it just tells Drupal that the content needs to be re-indexed. The actual index process happens when you run cron.

Create Search View Page

Now that we have the core Search module installed and working, we’ll spend some time creating the actual search page using Views.

1. Go to Structure -> Views (admin/structure/views) and click on “Add new view”.

2. Fill out the “Add new view” form, using the values defined in Table 1.0 and click on “Continue & edit”.

Table 1-0. Create a new view

Option Value
View name Search page
Machine name search_page
Show Content of type All sorted by unsorted
Create a page Checked
Page title Search page
Path search-page
Display format Unformatted list (default)

3. Click on the Add link within the “Filter criteria” and find the “Search: Search Terms” filter and click on “Apply (all displays)”.

Fig 1.1

4. On the filter configuration page, check the “Expose this filter to visitors, to allow them to change it” checkbox and “Apply (all displays)”.

Fig 1.2

All we have actually done is added the “Search: Search Terms” filter and exposed it so that users can enter in search keywords. The text field will appear above the views page.

At this point, we have a working views page. Save the view, and go to /search-page.

If you enter in some keywords into the “Search Terms” text field, the page should return some results.

Fig 1.4

Display Filter In Block

By default, views places the “Search Terms” text field (exposed filter) above the content area of the page. Views offers you the ability to expose the filter as a block. This is useful if you want to move the exposed filter somewhere else instead of just above the content area.

1. Go to the edit page for the “Search page” view.

2. In the Advanced section, click on the No link next to the “Exposed form in block” label.

Fig 1.5

3. Select Yes from within the pop-up box and click on Apply.

Fig 1.6

4. Once you have configured the view click on Save.

Assign Block To Region

You may notice that the filter is not displaying above the content area, that is because it’s now exposed as a block. The last bit of work we need to do is add the exposed filter block to a region within the theme.

1. Go to Structure -> Blocks and assign the “Exposed form: search_page-page” block a specific region.

Fig 1.7

2. Now, if you go to the search page, the filter should be in the assigned block.

Fig 1.8

The last bit of work required is to style the search results. But as you have seen, it’s pretty easy to create a custom search page using just Views and the Search module.

If you have any questions, please leave a comment.

About the Author

Ivan is the founder of Web Wash and spends most of his time consulting and writing about Drupal. He's been working with Drupal for 10 years and has successfully completed several large Drupal projects in Australia.

Leave a Reply 55 comments

Supriya Rajgopal - July 4, 2013 Reply

Thanks Ivan..
You saved me from hours of work!
Sometimes, solutions are so simple but we tend to keep complicating things!

Thanx again! 🙂 🙂

Duncan - July 9, 2013 Reply

Thanks and cross-referencing to your other articles is helpful too.

William - July 18, 2013 Reply

On the display content as block you could add that this option is under advanced. Small info, big help.

    Ivan Zugec - July 18, 2013 Reply

    Thanks for the tip. I have updated the text.

Lars Bo - July 26, 2013 Reply

Thank you for the tutorial. Nice and simple. This also offers a way to display search results only in the current language on multilingual sites. I used Display Suite Search for that in the case, I’m working on right now, but still 🙂

    Ivan Zugec - July 27, 2013 Reply

    Thanks that’s good to know.

Jesus Verde - August 2, 2013 Reply

I have been looking for something like that for hours, you make it so clear… thanks a lot!!!

Joe - August 24, 2013 Reply

Hi Ivan,

I landed up at your site after somehow already implementing the above but found that this works for only single words. If 2 or more consecutive words are entered such as say – leading provider – then there always gets to be no result.

    Ivan Zugec - August 25, 2013 Reply

    The views filter does not limit you to single words. There must be an issue with the search index on your site. Try reindexing the content on your site.

      Joe - August 26, 2013 Reply

      No, but i got it unbelievably happening by comparing the sql when a single word is entered vs when 2 words where entered and found the difference as ‘Count > 1’ vs ‘Count > 2’ for a Having clause (Sorry i don’t have the energy now to get the exact text) and then inserting the following code in a module file in the hook_views_query_alter(&$view, &$query) fn after doing a dpm($view->query) analysis:
      if ($view->name == ‘newsbin’) {
      if (isset($view->query->having[1][‘conditions’][0][‘value’])) {
      $view->query->having[1][‘conditions’][0][‘value’][‘:search_index_keys’] = 1;
      }
      }

      I still have a couple or more problems though one of them being when trying to find the whole phrase by enclosing the phrase within double quotes, there gets to be a very large number of duplicates.

      Another being that if both the words are found then there gets to be a duplicate.

      Hellooo.

Erik - August 25, 2013 Reply

I was looking for a way to keep some taxonomy terms out of my search index. But that seems hard. Second best option is to keep them out of the visible search results. Using a view would be perfect.

I do have the same problem as Joe however. If I search for two words I get an empty result. I have one node that had a name wich includes one letter, such as “place b”. If i search for those two words I get tens of results, all the same node.

I did a reindex of the search index, and i made a fresh view with filter search terms and exposed it. I actually tried this solution earlier and abandoned it because of the 2 keyword problems. Reading the above it seems this is not normal. But I cannot find the cause of this behaviour.

    Ivan Zugec - August 25, 2013 Reply

    If you guys are having problems that can be replicated, then create an issue in the Views issue queue.

    Also, found this in the Views issue queue about the search term problem:

    https://drupal.org/node/1948510

    Hope this helps.

      Erik - August 25, 2013 Reply

      Great find. The patch in that url fixed the problem for me. This opens a whole lot of possibilities to customize the search page. In my case easy ways to exclude taxonomy terms and node-types from search result (although I keep looking for a way to keep them out of the index in the first place).

      A thing I tried is to add a contextual filter search terms with argument search/node/%. This could be used to overwrite the core search results. However, core uses %20 between all keywords while Views seems to work with / between the different words. Looking for a solution to this. Or I just use the exposed block.

Ron - August 28, 2013 Reply

For some reason, setting ´On empty input´ to – `Show None´ still shows results…

What am I missing?

kkatusic - August 29, 2013 Reply

How we can set up search by term to search by only five character of term, for example “wor” would search for any word that begin with “wor”, like “Regular expression” in other exposed filter options?

Thank you

    Ivan Zugec - August 29, 2013 Reply

    Not sure how to do it. The “Search Terms” filter will only filter using whole terms. If you find a solution let me know.

      kkatusic - August 29, 2013 Reply

      Thx, for repluy, if I find solution I will post it here.

Marvine - September 25, 2013 Reply

Very useful 🙂

Ambica - October 10, 2013 Reply

Hi,
I followed your procedure, it is good but when i configure the search fields ie,exposing the field it always in a uncheck mode

    Ivan Zugec - October 10, 2013 Reply

    What do you mean by “exposing the field it always in a uncheck mode”.

    Are you talking about a checkbox? If so, which one?

Ambica - October 11, 2013 Reply

Yes,It is the checkbox. My theme is tb_neris i think it is not supporting the view search and it is displaying for content and users and after admin logouts there is search form

    Ivan Zugec - October 11, 2013 Reply

    Switch to the default Bartik theme and see if you have the same problems.

mrupal - November 5, 2013 Reply

Hi Ivan,

thanks for this (next) cool tutorial.

Do I understand it right – this exposed form is only usable if I am on the views’ search page itself.

If I place the block on the frontpage for example, it does not function, as all it does is add the filter identifier and the search phrase to the url, without actually redirecting to the search page: http://www.example.com/?keys=blabla and not http://www.example.com/search-page?keys=blabla

Is there any workaround for this?

Thanks again.

    Ivan Zugec - November 5, 2013 Reply

    It should redirect to the page. Is “/search-page” an actual views page display? Or is it a Panels page? Or something else.

    Test this out on a fresh Drupal 7 site and see if you get the same problem.

      mrupal - November 5, 2013 Reply

      Hi Ivan,

      you’re right, it’s panels that makes the trouble. On a fresh drupal site everything works fine…

      Seems somehow that the exposed block doesn’t know where to redirect if this block is being rendered by a panel. I’ve tried various different approaches (like displaying the results in a views pane which is rendered by a page manager custom page) but with no luck.

      Meanwhile I’ve found a solution by putting something like this in a custom module (assuming it’s the initial approach with a views page and not a page manager page and the block rendered by a panel):

      // Search by views: redirect to the views results page on submitting the exposed block form
      function my_module_form_views_exposed_form_alter(&$form, &$form_state, $form_id) {
      $form[‘#action’] = “/” . $form_state[‘view’]->display_handler->options[‘path’];
      }

      If I find more time I will try further to achieve this without having to code and will give some feedback. If someone has some experience with that, please give a small hint. 🙂

      Thanks

        Ivan Zugec - November 5, 2013 Reply

        Thanks for the update. 🙂

Manny - November 13, 2013 Reply

Is it possible to run a search with no terms(blank search box)?
I’d like the search to return All results of the chosen content types if no keywords are entered into the search and have tried a few different ways to do this with no avail.
This article a great help!

KCl - April 1, 2014 Reply

I cannot thank you enough for this post. Over a year ago, I gave up after trying everything (or so I thought) to get a view to have a search box that searched all fields. I stumbled on this post today and I believe it has solved my need in under 5 minutes. If there is a way to give it more visibility, I would do so. Heartfelt thanks.

    Ivan Zugec - April 2, 2014 Reply

    My pleasure, thanks. 🙂

      KCl - April 2, 2014 Reply

      The search operator appears to be OR when the core search is AND. Is there some way to use Search: Search Terms with an AND by default? I saw https://drupal.org/node/356998 for Drupal 6 but adding AND between the terms does not AND them in my Drupal 7 install.

        Ivan Zugec - April 4, 2014 Reply

        I’m not really sure how to do this in Drupal 7. The best place to ask would be in the Views issue queue.

Sillo - April 28, 2014 Reply

just what i was looking for!, thanks alot!

Erin - April 28, 2014 Reply

Hi there,

I’ve set up my search exposed filter and view, however it seems the pager does not work. If I set the pager to display 30 items, it will show the first 30 and there is no pager at the bottom. Have you experienced this? Currently I have it set to “View All” but the results list becomes very long.

Any help is greatly appreciated.

Thanks,

Erin

    Ivan Zugec - April 28, 2014 Reply

    Have you done any template overriding or modified Views. Try creating a basic view and see if a pager appears.

      Erin - April 30, 2014 Reply

      I tried creating a basic view and the pager worked. So then I tried cloning my existing view and the pager worked again! No idea what happened there but I got it working. Thanks for the tip!

rebecca - July 11, 2014 Reply

Hi!
How can I theme the search result of the exposed filter form? The exposed filter form is displayed in a block, and it has something else on the page. If I filter and search, there will be only the result. I also want to show a menu on the left part of the page. What can I do?

    Ivan Zugec - July 12, 2014 Reply

    If the exposed filter is being displayed as a block, then all you’ll have to do is display it on the search page. Just assign it to a region via the Blocks page in the admin section.

Syed Atif - September 18, 2015 Reply

Hi Ivan Zugec!

I have tried above tutorial and I can see that it works perfectly. But what I need is to keep the default drupal search as is (I have themed search–results.tpl.php) but on search page I want to display just titles of all articles when no search has been made.
Any clue on how to do it?

    Ivan Zugec - September 19, 2015 Reply

    Hi Syed,

    I may be wrong, but I don’t know if it’s possible using core Search. However, you can do this using Search API and Views.

Alex - November 12, 2015 Reply

Hi, is there a way to show your searched term somewhere above the results?

    Ivan Zugec - November 14, 2015 Reply

    Hi Alex,

    If you don’t want the “Search terms” block in appear in the sidebar then leave “Exposed form in block” to No. This will leave the exposed block above the search results.

    Ivan

Esther - July 21, 2016 Reply

Hi, thanks for this walk through. Can you please let me know how to go about making this search for only users of the system.

Thanks

Puia chhakchhuak - August 23, 2016 Reply

When i directly enter the views page url without a form submission, i get this:

Notice: Undefined index: keys in __lambda_func() (line 2 of /var/www/html/sitefolder/sites/all/modules/views_php/plugins/views/views_php_handler_area.inc(39) : runtime-created function).

I have looked around, but none of the solutions I have found deal with search terms. Does any one know a solution.

Thanks

Shruti - February 15, 2017 Reply

Hi,

I have tried above tutorial and its working perfectly but the problem is I’m getting the duplicate search result. Can you please help me out.

Thank you in advance

    Ivan Zugec - February 15, 2017 Reply

    Hi Shruti,

    Never had this problem. Try setting the distinct option in Views to remove duplicates.

    Cheers,
    Ivan

Leave a Reply: