Home > Best practices, Drupal, English > Create a view to search on content title in 7 steps in Drupal 7

Create a view to search on content title in 7 steps in Drupal 7

I searched for this a long time just to find it was very easy to do, so I hope this helps you too.

The objective of this short guide is to explain how to build a view in Drupal 7 that will let you search for part of a title of a specific content. Let’s say your site is about music and you have a content type called “Song”, which has an artist and a title. Now let’s say you want to search on the title and let the user select a search term.

It’s very easy to get distracted by the “Contextual filters” option. If you wonder what this is, I recommend you have a look at this excellent video tutorial on Drupal 7 views by Johan Falk. There is also a long series of Drupal views tutorials linked to that one.

Now if you want to do what we suggested, there are only a few short things to do:

1. Create a view

To create a new view (you’ll need it), follow the Structure -> Views menu and click Add new view link (see red underline in screenshot).

2. Define view settings

The view settings should be defined to what suits you best. Let’s say you want to change the title, the URL and give it a specific content type to look for. See options underlined in red in the next screenshot. The”Title (links)” option is selected as a display format so that the results will link directly to the song.

3. Add fields

Once the basic parameters of your view have been set, you can add the fields you want to show in the results list. Here, we added the artist field.

4. Select content title filter

Now comes the real trick. In order to allow users to search for your songs titles, you will need to “expose” the corresponding title filter to your users. In order to do that, you first need to have a song title filter. That’s what you’re going to add clicking on the “Add” button next to “Filter criteria”. In the filter types list, select Content: Title (as you are already inside a view on the Songs content type.

Click “Add and configure filter criteria”.

5. Set the filter settings

You’ll first get a form like this:

Update all the settings (starting by the “Expose criteria” checkbox) so you get something similar to this:

You want to pay attention to the filter type, which should say “Contains”, so that any *part of* a title will give you the song with that title. To save, click “Apply (all displays)”.

6. Save the view

Your view should now look something like this. Just click the “Save” button on the top of the view to apply your changes.

7. Enjoy

Now click the “View page” button on the right side of your view panel. This will lead you to a page with… a search box and a list of songs! Well done!

Categories: Best practices, Drupal, English Tags: ,
  1. dropby
    March 9, 2012 at 1:37 am

    thank you very much for this tutorial :)

  2. billy
    March 31, 2012 at 6:15 pm

    THANK YOU!!!
    After some 2 hours wrecking my brain on D7 Views, your tutorial helped 100%.
    Peace brother, luck and good fortune to you.
    Good karma.

  3. Megan
    December 20, 2012 at 6:48 pm

    This is awesome! Is there a way to display this as a block, and the once you click apply it goes to a results page?

  4. Tirthrajsinh
    January 17, 2013 at 9:14 am

    Thank You…

  5. February 6, 2014 at 6:47 pm

    How would you get a search term, aka arg(2), be the keyword for Content: Title contains?

  6. May 29, 2014 at 7:11 pm

    Thanks a bunch, helped me!

    (i forgot to choose ‘contains’ instead of ‘is equal to’

  7. drupaldeveloper
    July 14, 2015 at 3:38 pm

    Great post! Saved me! After lot of searching stuff this is really useful!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: