Multi parameter search in Craft CMS
Tags: craftcms, twig, webdev, contentmanagements
Published: 2019-04-18 13:22:35

---
title: Multi parameter search in Craft CMS
published: true
tags: craft-cms,twig,web-development,content-management-s
canonical_url: 
---

 **The challenge ** — an attorney search feature that would search by any or all of the following:

- Attorney Name
- Attorney Practice Area (category)
- Keyword

![](https://cdn-images-1.medium.com/max/330/1*F5Qd5Ia0a20szbCgXQbn2w.png)

**Populate the Category Dropdown**

The first step is building the search form. We just need to add the additional fields to the typical Craft CMS search form. In this example we have a section on the site for Practice Areas. Each entry is a different practice area and we can populate a drop down field with the titles for each of these Practice Area entries.

![](https://cdn-images-1.medium.com/max/1024/1*vx3nlus2fF8Z4OtYZm8IIQ.png)<figcaption>Practice Area Field populated by Practice Area section of site</figcaption>

The keyword field is simply the default search field and the Attorney Name field is and additional basic text field.

![](https://cdn-images-1.medium.com/max/1024/1*xxpCJCIihpNTGEcUDChHZg.png)<figcaption>Keywords and Attorney Name Search Fields</figcaption>

**Set the Search Parameters**

In typical fashion a user enters their search parameters and clicks the search button.

What actually happens is that the Craft CMS form builds a URL with the search parameters that were entered in the form and it comes out looking something like this: [http://domain.com/search?section=attorneys&practice=Family+Law&q=family&name=smith](http://domain.com/search?section=attorneys&practice=Family+Law&q=family&name=smith)

**Extract the Parameters from the URL**

![](https://cdn-images-1.medium.com/max/1024/1*StCFMPgitk1duZ-f_jq0aQ.png)<figcaption>Get all the parameters from the URL</figcaption>

Using the handy dandy [craft.app.request.getparam](https://docs.craftcms.com/api/v3/craft-web-twig-variables-request.html#method-getparam) public method we can get those search terms out of the URL query string.

**Build a Search Query with these Parameters**

![](https://cdn-images-1.medium.com/max/1024/1*GTnq5bA6xI4QT7MqfbocQA.png)<figcaption>Build an array containing all search parameters</figcaption>

We want to check that each parameter is defined before we merge it into our main queryParams variable. Craft CMS uses “AND” by default for multiple search parameters so all of our parameters will be included in the search.

We also decided display the search parameters back to the user’s screen.

![](https://cdn-images-1.medium.com/max/1024/1*afbtMc_opzGkyW0sgZS7Aw.png)<figcaption>Display the user’s search parameters</figcaption>

We can then run the search, using the Craft CMS pagniation to display the results. In this specific case we also check for an additional parameter of “sectionHandle” which is only defined if we are using the Attorney Search feature to search withing the Attorneys section. We can use this same search template when conducting a regular site search. In that case we won’t have the sectionHandle defined and we will search the whole site.

![](https://cdn-images-1.medium.com/max/1024/1*RyNDSG9qvDZim8eUYEL67A.png)<figcaption>Using the search parameter array, run the search (with pagination)</figcaption>

**Finally Display the Search Results**

Now we can display the search results in whatever layout we choose, similar to the default [Craft CMS search](https://docs.craftcms.com/v3/searching.html#searching-for-specific-element-attributes) results.

![](https://cdn-images-1.medium.com/max/1024/1*xJ6CRC3rBrcOZr-fjs6OHA.png)<figcaption>Display the search results</figcaption>

* * *