Filter Criteria

In the video Modifying Default Views, I showed you how to add a filter that limits the displayed results of the view by content type. Now, let's add an exposed filter. Exposed filters allow users to select filter options. Let's add an exposed filter to allow users to choose to display all items, only available items or items that are not available. To do that:

  • Click "Add" next to Filter Criteria
  • [x] Content: Availability (field_availability)
  • (Apply (all displays))
  • [x] Expose this filter to visitors, to allow them to change it
  • Label: Filter by Availability
  • Operator: Is one of
  • Options (If you select one of more options here, you can choose to limit the view to only content that matches the options selected. I'll leave all items unselected so that they are all available.)
  • [ ] Expose operator (If you expose the operator, the user can select the operator to use. ie: "Is one of", "Is all of" etc.
  • [ ] Allow multiple selections (This allows the user to select more than one option. In our case, that doesn't make sense, but if you had options like: Red, Green and Blue, you might want to allow users to select multiple options.)
  • [ ] Remember the last selection (This will cause the View to remember the user's last selection so that if they leave and come back, they can resume where they left off.)
  • [ ] Limit list to selected items (This is the option that limits the view results to those selected above.)
  • [ ] Reduce duplicates (If you have a list of content that can have more than one option, a single piece of content might show up more than once. (ie: If the content has both the "Red" and "Green" taxonomy terms, it would be listed twice. Once for each term.) This option will limit each piece of content to only display once regardless of how many terms it matches.)
  • (Apply (all displays))

  • (Save) (Save the view)

Now, when we scroll down to the live preview, we'll notice that the "Product" view has an additional region for Exposed Filters. Let's try it out, in the dropdown box, I'll select "Available" then click "Apply". We'll see that, now, it only displays available products. Next we'll take a look at the Sort Criteria section.

Sort Criteria

Sort criteria lets you sort the results of the view before it is displayed. You can sort on none, one, or several aspects of your content.

  • Click "Add" next to Sort Criteria
  • Filter: Content
    * [x] Content: Price (field_price) (This will sort the results according to Price)
  • (Apply(all displays))
  • [x] Sort Ascending (We'll use the default option to sort items by lowest to highest price. This would be a good place to expose the filter, but for demonstration, I won't.)
  • (Apply(all displays))

Now, if we take a look at the live preview, you'll notice that the products are not listed from lowest price to highest. This is because we have two sort criteria. Views will apply sort criteria in the order they appear in the list. So our view is sorting the content by post date. Then, content that has the same post date is sorted by price. We could rearrange the criteria so that price is sorted first, but I'd rather just get rid of the post date sort criteria.

  • Click "Content: Post date (desc)"
  • (Remove)

Now, when we scroll down and look at the preview, we'll see that the products are now displayed from lowest to highest price. (You'll also notice that the two items of the same price are sorted alphabetically. This is the default behavior of Views.)

Before we proceed to Page settings, let's see the "Product" view rendered in a full page display.

  • (Save) (Save the view)

  • Click "View Shop Page"

This is how our view looks with our default theme applied.

In the next video, we'll continue to customize the view using the page settings.