2

I am looking for a way to produce a dropdown filter by year of drupal 'post date'. i was able to produce somewhat of this result with an exposed criteria but that requires updating.

Is there a way i can just make the sort criteria exposed with dropdown of years?

this is what i have so far

if ($form_id == 'views_exposed_form_clone_of_post_by_date_block'){ 
  $year_filter = array();
  $years = array(); 
  $counter = 0; 
  for ($i=date("Y");$i>=2009;$i--){ 
    $counter++;
    $years[$counter]=$i; 
    $dstart = $i."0101"; 
    $dend = $i."1231"; 
    $year_filter[$counter] = array('title'=>$i,'operator'=>'between','value'=>array('type'=>'date','value'=>$‌​i,'min'=>$dstart,'max'=>$dend)); 
    $form_state['input']['edit_created']['#options'] = $years; 
  }
0

2 Answers 2

4

How to produce a dropdown filter by year of post date isn't as immediately obvious as you might think - but it doesn't need any custom code when using Drupal 7 with views.

Within your view add a new filter. You might think you need to choose 'Content: Post date' but this will not give you the options you need. You should actually choose 'Date: Date (node)'. For data selection form element choose 'select' to create your dropdown. Set Filter granularity to 'year'. Give a starting year e.g. '-10 years from now' and an ending year e.g. '+ 0 years from now'. Then in 'date fields' you select where the date(s) are drawn from, it is here that you choose the 'content: post date'.

It is worth noting that other date fields are exposed here, so you are not limited to the post date.

2
  • I don't see Date:Date(node) in my filter listing options Commented Nov 28, 2014 at 19:15
  • @JoshuaKissoon - You need the Date module enabled.
    – leymannx
    Commented Jul 18, 2016 at 14:01
0

Bit old question, but in case someone else is looking for the "Date: Date (node)" filter: This filter will be only available if you enable date_views module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.