Search Form restricted to particular node type, in a block.

A site I’m working on has quite a few search forms. Basically, for each of the node types, we want to give the user an easy option to restrict search to only that type, ie events, blog posts, as well as a few custom content types. Sometimes the node types will be presented by taxonomy term, so that the user should be able to easily search through events tagged “toddler,” for example. Here’s how to construct these search boxes and place them into blocks.

First, construct a view which lists all of the content you’d like to search together. For example, I’ve created a view called Event List. In it, I filter based on node type == event. I add another filter from the Search group, “Search: Search Terms”. This filter is exposed and optional. On “empty input”, select “Show All”. I created an argument for the taxonomy term and also choose a few fields. Then, I created a page view and gave it the path “events/%”. In the Basic Settings fieldset, change the setting “Exposed form in block” to “yes.” Save!

Now, when you go to the Blocks page, you’ll see a block called “Exposed form: Events-page_1″ (or something nicer if you name your page). You can just drag it to a sidebar for testing, but later on you’ll probably want to restrict where this block appears.

When the block is displayed on /events/all, it searches all events. When it is displayed on events/toddlers, it only searches within events tagged toddlers. Just what we wanted.

Discussion Area - Leave a Comment