[Drupal 7][views 3.x] How to programmatically pass date to an exposed date filter in views

| | 1 min read

How to programitcally call a view using exposed filter was discussed earlier. But if you want to pass a date field to the view's exposed filter form, you have to change your code a little bit. If you want to know how, you may read on.

Suppose you have an exposed filter of content 'date-of-birth'. Let the id of this exposed filter be 'field_date_of_birth_value'. To pass a value to the date-of-birth's exposed input, add the below code to your function:

case 1: If the date selection form element of your filter is a 'Text field' or 'Pop-up':(Refer screenshot.png)

$view->exposed_input['field_date_of_birth_value']['value']['date'] = '2013-01-02';//[format:'YYYY-M-D']

case 2: If the date selection form element of your filter is a 'Select List'

$view->exposed_input['filter's id']['value']['year'] = '2013';
$view->exposed_input['filter's id']['value']['month'] = '2013';
$view->exposed_input['filter's id']['value']['day'] = '2013';

By adding the above code, you will be able to see the view filtered by the date_of_birth value.

A small tip:
If you dont know the id of your filter, you may find using:

  1. Click on the exposed filter whose value you need to know » click more » The field 'Filter identifier' has the value of your filter.
    OR
  2. By using dpm($view->exposed_raw_input);

Hope this was useful!!