Last updated June 4, 2010. Created by merlinofchaos on March 17, 2006.
Edited by RoloDMonkey, greggles, add1sun, hunmonk. Log in to edit this page.

Filters are what are used to reduce the result set of a View to a manageable amount of data. They are a critical part of the foundation of this system.

The filtering system is a compromise between flexibility and ease of use. To be truly powerful, filters should provide the ability to do an OR, but as it stands, these filters are all ANDed together. That means that every filter is applied. While the ability to do an OR would be nice, unfortunately the level of complexity in the interface to do that is well beyond the benefit that would be given.

The order of filters is irrelevant, though they can be re-ordered as a convenience.

Field
This is simply the field being filtered on.
Operator
The operator tells Views HOW to filter, and is part of the 'equation' of a filter. Usually this appears as a human readable sentence, and by simply reading the fieldname, with the operator and the value, it is clear what the operator means. For example, "Taxonomy Term", "Is One Of", "...some list of selected terms...".
Value
The Value field varies greatly based upon what field is being filtered, but is usually either a text box or a select box.
Option
The option field is specific to each field being filtered. The only currently existing option is on taxonomy terms, where the taxonomy depth is listed as an option. For taxonomy filters, you cannot specify "all" as a depth but must specify a number. The correct number to use depends on your site, but 2 will get roughly 90% of the uses. 3 will work for roughly 99% of the cases. The more you set the more expensive the query so set it to the smallest number that will work for your situation.
Expose
If a filter is 'exposed' the user will be presented with a widget to control the filter. When the expose button is clicked, the filter is added to the 'Exposed Filters' fieldset that appears just below the Filters.

Exposed Filters

Exposed filters are shown to the user, so that the result set can be modified. The typical example of this is to expose taxonomy as a filter so that the user can select which taxonomy term to filter on.

The order of the exposed filters controls the order in which they are presented to the user.

Field
This is the field being filtered; while all of the other fieldsets include an 'Add' button beneath it, this one does not; to add an exposed filter, go up to the Filters fieldset and click 'expose' on the filter you want exposed.
Label
This is the label presented to the user.
Optional
If a filter is set optional, an "<All>" choice will be added (if it's a select). If a filter is not optional, a value must be provided to the view.
Filter settings Default
If a filter is set to optional, and this setting is checked, the actual settings of the filter will be the default; if this setting is not checked, the default will be set to blank or .
Force Single
If this setting is checked, any multi-select box available for this filter will be forced into a dropdown box and only one selection will be allowed. This is typical; dropdowns use less screen real estate and are easier for people to use.
Lock Operator
If this field is checked, the 'operator' will not be exposed, and the setting in the Filters section will be used. This setting is typical.

What follows is a list of filters that Views provides by default. Like other systems in Views, modules can provide more of these.

Node: Has New Content
Including this filter will reduce the node set to nodes that have been updated or have new content since the user last read the node, as well as unread nodes.
Node: Published
Filter by whether or not the node is published. This is recommended for most Views!
Node: Front Page
Filter by whether or not the node has been promoted to Front Page.
Node: Sticky
Filter by whether or not the node is set sticky.
Node: Moderated
Filter by whether or not the node is moderated.
Node: Type
Include or exclude nodes of the selected types.
Node: Author is Anonymous
This allows you to filter by whether or not the node author is anonymous.
Node: Author is Current User
This allows you to filter by whether or not the node was authored by the logged in user of the view.
Node: Current User Authored or Commented
This allows you to filter by whether or not the logged in user authored or commented on the node.
Node: Distinct
This filter ensures that each node may only be listed once, even if it matches multiple criteria. Use this if multiple taxonomy matches return duplicated nodes.
Node: Author Name
This allows you to filter by a particular user. You might not find this useful if you have a lot of users.
Role: Author Role
Include the node only if the author is a member of the selected role.
Taxonomy: Term
When filtering by taxonomy term you may specify the "depth" as an option. Please see the taxonomy help for more information.
Taxonomy: Terms for a specific vocabulary
Only terms associated with the specified will appear in the select box for this filter. When filtering by taxonomy term you may specify the 'depth' as an option. Please see the taxonomy help for more information.
Taxonomy: Vocabulary Name
This will filter a view to only nodes that contain a term in the associated vocabulary.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

I misunderstood the logic of "Greater Then or Equal to" and thought a quick note might save others a little time.

The set-up: a simple view of basic job descriptions, with two fields in the filter section: salary-start, and salary-end, representing the salary range of the job ("This job pays between $30,000 and $40,000"). Both fields are integer fields created in CCK.

The goal: Allow users to search for jobs above a certain salary. For example, if I entered $32,000 in the search field, I'd expect to find all the jobs greater then or equal to $32,000.

What I did wrong: I exposed the salary start field and selected the operator "greater then or equal to". I realized after a few searches that not all of the appropriate records where turning up in my search. For example, I searched on $32,000, and yet a job that started at $30,000 was not showing up. Why was this not working? Wasn't $32,000 larger then $30,000?

The problem was my logic. When I exposed the salary-start field, I thought I was asking "Is my search term ($32,000) greater then or equal to the salary-start field of the job record?" Remember our sample record has a salary-start value of $30,000.

The revelation: What was really being asked is "Is the salary-start field ($30,000) greater then or equal to my search term ($32,000)?" The answer is, of course, no. So the record wouldn't show up in the found set.

The fix: Change my logic. I un-exposed the salary-start field, and instead exposed the salary-end field. Our sample record has a salary-end value of $40,000. Now the logic is "Is the salary-end field ($40,000) greater then or equal to my search value ($32,000). Since the answer is yes, the record is included in the found set.

I was confused by the interworkings of Filters and Exposed Filters. When you Expose a filter, it appears in both lists. The choices you make on the non-exposed version apply to the exposed version as well, as defaults in some cases, as parameters in others. If you delete the non-exposed version then you lose control of the Operator and Options fields, and possibly others. Hope this helps someone else in the future.

The documentation should have some comments on how to use the patterns. I eventually (trial and error) figured out something that works, but there may be more that can be said.

Select "matches pattern" and then put a SQL pattern in the value field without the LIKE or quotes. For example to match any text field that begins with W the SQL pattern would be LIKE 'W%' but for the value field in views all you put in is W%.

Walt Daniels
Webmaster nynjtc.org

* Rewrite of my previous post... it was too long and obscure *

Is it possible to create a drop down list box/select list exposed filter for a CCK Integer field.

I need an exposed filter on an integer field where users will only enter values between 1 and 10, and I want to show an exposed filter greater than filter using select list ie Show results where "my integer field" is > 2 for example.

Any help very appreciated.

rob | www.beetleweb.com

I am not sure about 5.x, but in 4.7 the exposed filter shows all the filter optiosn, regardless wether or not you set it to a subset of all options. This form_alter snippet changes that behaviout, to the IMO more appropriate subset of filter options.

---
Professional | Personal

Hi

I am trying to implement a real estate search where I need select boxes for price range(min and max range), bed (greater than value) , bath (greater than).

I wrote my own module to implement the price range as I could not find anything else. I see the form populating on my search block and when i print the min and max it does show the value but it is just not filtering anything. Can some one plz help me with this.

The code is as follows:

<?
/**
* Implementation of hook_form_alter().
*/
function custom_filter_form_alter(&$form, $form_state, $form_id) {
// Load up a $view object like we would expect to work with
if (isset($form['#parameters'][1]['view'])) {
$view = $form['#parameters'][1]['view'];
}
switch ($form_id) {

case 'views_exposed_form':
if ($view->name == 'view_search_block') {
custom_filter_range_to_select('field_ohouse_price', array(
'0,999' => 'Less than $1,000',
'1000,1999' => '$1,000-$1,999',
'2000,3999' => '$2,000-$3,999',
'4000,6999' => '$4,000-$6,999',
'7000,999999' => '$7,000 plus',
), $form, $form_state);
}

break;
}
}

/**
* Turn a range field into a select dropdown. This assumes that the $options array
* is going to be something like: array('5,9' => '5 to 9 lbs') where the index is
* a comma delimited string of the min/max values.
* Pass in $optional = TRUE if you want there to be an value at the top of
* the select dropdown. Defaults to TRUE.
*/
function custom_filter_range_to_select($field, $options, &$form, &$form_state, $optional = TRUE) {
$form[$field]['#type'] = 'select';
if ($optional) {
$options = array_merge(array('All' => ''), $options);
}
$form[$field]['#options'] = $options;
unset($form[$field]['min']);
unset($form[$field]['max']);
$f = $form_state['input'][$field];
$f ? $form[$field]['#value'] = $f : true;
$form[$field]['#element_validate'] = array('custom_filter_range_validate');
}

/**
* Turn values created by range_to_select back into ranges so that Views can process
* the request. This assumes that if the value passed in is 'All' the min/max array
* should be set to array('min' => '', 'max' => '')
*/
function custom_filter_range_validate($element, &$form_state) {
if (($v = $element['#post'][$element['#name']])) {
if ($v == 'All') {

$min = $max = '';
}
else {
list($min, $max) = explode(',', $v);
}
$form_state['input'][$element['#name']] = array(
'min' => $min,
'max' => $max,
);
$form_state['values'][$element['#name']] = array(
'min' => $min,
'max' => $max,
);
}
}

Prerna.

The manual says:

The operator tells Views HOW to filter, and is part of the 'equation' of a filter. Usually this appears as a human readable sentence, and by simply reading the fieldname, with the operator and the value, it is clear what the operator means. For example, "Taxonomy Term", "Is One Of", "...some list of selected terms...".

But what is supposed to be "human readable"? Examples:

* I'm trying to filter a date field; I want to display all records matching "today" or seven days earlier. Operator "greater than or equal to" with Value "now" outputs dates in the future; but neither an expression like "now-7" or "seven days before now" works.
* I want to filter all records matching "current month, from the beginning". Of course, this value isn't acceped, also

So which values are valid for the operator?? There's for sure a reference for this, right???

Thanks, -asb

If you want to show only nodes that are not older than 7 days, you have to add:
- Filter: "Node: Created Time"
- Operator: Is Greater Than (or Is Greater Than Or Equals)
- Value: now
- Option: -604800 (time is calculated in seconds - 604800 means: 7 days * 24 hours * 60 minutes * 60 seconds)

It works perfectly for me.

(EDIT) About filters, a patch exists for the views.module v.5.x-1.6: See "Missing taxonomy terms in filters": http://drupal.org/node/199675

I wanted a table view that gave me the previou n weeks of data, but setting variables in the argument handling code didn't seem to work at all, and the arguments didn't seem to be visible in other parts of the view.

After fussing for quite a while, I realized the trick was to forget about passing variables to the filters and to modify the filter directly in the argument handling code, instead. Thus, my final view had this argument handler:

$view->filter[1]['options'] = $args[1] * -604800;

This modified the default filter, replacing the default value (-16 weeks, measured in seconds) with the URL argument multiplied by the number of seconds in one week.

Here's the whole form:

  $view = new stdClass();
  $view->name = 'bikelogchart';
  $view->description = 'Bike Log Chart';
  $view->access = array (
  0 => '1',
  1 => '2',
);
  $view->view_args_php = '';
  $view->page = TRUE;
  $view->page_title = 'Rolling Bike Log';
  $view->page_header = '';
  $view->page_header_format = '2';
  $view->page_footer = '';
  $view->page_footer_format = '2';
  $view->page_empty = '';
  $view->page_empty_format = '1';
  $view->page_type = 'table';
  $view->url = 'view/bikelogchart';
  $view->use_pager = FALSE;
  $view->nodes_per_page = '0';
  $view->sort = array (
    array (
      'tablename' => 'node_data_field_ridedate',
      'field' => 'field_ridedate_value',
      'sortorder' => 'ASC',
      'options' => '',
    ),
  );
  $view->argument = array (
    array (
      'type' => 'uid',
      'argdefault' => '1',
      'title' => '%1\'s Recent Bike Log',
      'options' => '',
      'wildcard' => '',
      'wildcard_substitution' => '',
    ),
  );
  $view->field = array (
    array (
      'tablename' => 'node_data_field_ridedate',
      'field' => 'field_ridedate_value',
      'label' => 'Ride Date and Time',
      'handler' => 'content_views_field_handler_ungroup',
      'options' => 'short',
    ),
    array (
      'tablename' => 'node_data_field_ridedist',
      'field' => 'field_ridedist_value',
      'label' => 'Distance',
      'handler' => 'content_views_field_handler_ungroup',
      'options' => 'us_1',
    ),
    array (
      'tablename' => 'node_data_field_ridespeed',
      'field' => 'field_ridespeed_value',
      'label' => 'Average Speed',
      'handler' => 'content_views_field_handler_ungroup',
      'options' => 'us_1',
    ),
    array (
      'tablename' => 'node',
      'field' => 'title',
      'label' => 'Summary',
      'handler' => 'views_handler_field_nodelink',
      'options' => 'link',
    ),
  );
  $view->filter = array (
    array (
      'tablename' => 'node',
      'field' => 'type',
      'operator' => 'OR',
      'options' => '',
      'value' => array (
  0 => 'bikelog',
),
    ),
    array (
      'tablename' => 'node_data_field_ridedate',
      'field' => 'field_ridedate_value_default',
      'operator' => '>=',
      'options' => '-9676800',
      'value' => 'now',
    ),
  );
  $view->exposed_filter = array (
  );
  $view->requires = array(node_data_field_ridedate, node_data_field_ridedist, node_data_field_ridespeed, node);
  $views[$view->name] = $view;

I spend some time searching for the answer on how to embed context-sensitive views with filters.
Filters need to be exposed.
Here is an example when searching for nodes that are created before currently displayed one

$view = views_get_view('next_nodes');
$formated_date = format_date($node->created, 'custom', "Y-m-d H:i:s");
$filters = views_get_filter_values(array('op0' => '<', 'filter0' => $formated_date));
print views_build_view('block', $view, $arguments, false, 1, 0, 0, $filters);

The best way to get correct values for option and filter is to decode URL that u get when using exposed filters on your View Page

Here's what i would like to do..do you think it could be done by what you're saying (i'm not a coding expert.. :( ) ?
http://drupal.org/node/365286

Great thanks for any thoughts!

I need a way of doing a "full-text search query" of taxonomy terms of a particular vocabulary (1200 of them, so no drop down or autocomplete), alongside other exposed views filters built around the views & fast search module

With a "node title" filter you can have the filter: is equal to, contains, contains any words, contains all words, starts with, ends with, does not contain

I have noticed this is impossible with the taxonomy filters.

Is there a work around to this????

See also this thread

http://drupal.org/node/318179

I am new to Drupal - I really like it!

I am building a view with a view type of node. I need to filter my nodes, which are classified according to a taxonomy. At view configuration time, I know the exact taxonomy term (I'll call it T for discussion purposes) that I wish to filter by. That I want to filter by T is static knowledge that I wish to build right into the view. The user's input is not required. I simply want to filter out nodes that are not classified as T. This does not appear to be possible, and seems it would be really neat if it were.

Meanwhile, I'm going to work around it by managing my own taxonomy-like CCK data.

I am answering my own post here.

I figured out how to do what I wanted. Turns out I was wrong. By not Exposing my Taxonomy Term filter, it worked exactly the way I wanted, and no user interaction was required when the display was rendered.

Newbie mistake.

Hi

I've cloned the glossary view and modified it to suit my needs. It works fine except when I reach the filter. If I do not have anything in the filter section, it displays everything how it should, except that it displays all the content I have. But I want to filter to only show a custom node that I've created, as soon as i check the following boxes:

- Is one of
- Custom node

all the results return blank, when I know I have 2 items that should be appearing in the list.

What am I doing wrong?

I created a view with exposed filters. I can go to the page and see the filters just fine and use them. That part works. But I need to be able to embed the view in a panel. So I added a block to my view but it doesn't show the filters from the default display? Is there something else I need to do to make them show up or is this a limitation of views as a block in a panel. Technically speaking can I have /myproducts/myproduct?tid=1 in a panel and have the view us it and set the filter value like I can in the page example that works for me. Hope this makes sense.

Thanks for any help.

Is it somehow possible to expose an Image Cache preset using an exposed Views filter so that the user can have the option of selecting a preset?

Any pointers on this would be appreciated . Thanks

Bhambry

Hi I am currently trying to put together an intranet for the college I work in. I have a number of panels that each display a view. I want to set up the views so that content is displayed depending on the users role. For example I have a news content type that I have added a CCK field 'Role Reference' that show a list of roles with checkboxes. The idea is that when news is created, the areas to which that news applies is 'checked' and then displayed using a view.

I've tried setting up the view to use this field as a parameter and using PHP code for the default parameter but I can't resolve the problem of:

a: the format that the incoming parameter is
b: the fact that the PHP code needs to return a single value when the parameter and the user represent multiple roles

I'm running Drupal 6 with CCK, Views and Panels

Can you or anyone help?

Hope I'm posting this in the right place :(
It maybe an omission on my part, but I don't see a way of having a view that has exposed filters to only show content when the filter is applied.
I would appreciate any suggestions on how to achieve the desired behavior.

Sergio

i have a view with several exposed filters, including a Date field, which i specified in the views ui as a date range. the 2 date filter fields show up as expected (from date and to date), but they are situated one above the other, and i want them to appear side by side.

i have torn apart everything i can think of, including views-exposed-form.tpl.php (which has the entire 2 date fields, labels, etc as html in $widget->widget) and various theme_form_element(). nothing seems to work. when i think i have discovered what css classes to float: left, it doesn't work.

can anyone help?

thanks!

I am having a similar problem where I have price range x to y and they show up one above the other. I have tried everything as well.. I thought it was something trivial that I am missing and would love to know as well if there is a solution to this...

Thanks,

Prerna.

i would have to find my ld code to provide you with specifics, but the way i worked around this issue was to hack away at the filter form elements in the template file that was displaying the view. i basically pulled each specific form element out of the muck that is drupal's generated code, and layed them out myself with css/html. seems absurd to have to do that, and maybe there is an easier way, but i couldn't find one.

I'm using Drupal Commons for the first time, and exposed filters show only under an expandable region called "Filters" which is very unintuitive. At the very least I'd like to set this region to be expanded by default to show the filters. Has anyone else encountered this issue? Is there a way to do what I'm suggesting? I am thinking that there should be a checkbox somewhere that will allow me to show as expanded by default, but I haven't found anything yet.

Posted to wrong page, sorry.

Saving the internet, one file at a time.
If I helped, Pay it forward or give me a cookie ;)

does anyone know how i can achieve this kind of filter in drupal 7 views:

check the currently logged in user for certain roles
then display a list of users who match those roles as well

so for example im logged in and i have 6 different roles, 2 of those roles are 'male' and 'food'
all users have 'gender' and 'hobby' field, so if i am a MALE who likes FOOD i want the view to show me FEMALES who also like FOOD
i then need to do this for all the different combinations thats possible using those two fields, whats the best way to do this?