Support

Account

Home Forums Backend Issues (wp-admin) year picker

Solved

year picker

  • Does anyone know if it’s possible to use a date picker with the year only(drop down with years) or should I use a select field type? Thanks!

  • I would use a select field.

  • I know this is a pretty old post, but if anyone else wanted the code to make this happen:

    function sevenYearSelect($field) {
        
        $currentYear = date('Y');
        
        // Create choices array
        $field['choices'] = array();
        // Add blank first selection; remove if unnecessary
        $field['choices'][''] = ' ';
        
        // Loop through a range of years and add to field 'choices'. Change range as needed.
        foreach(range($currentYear-1, $currentYear+5) as $year) {
                
            $field['choices'][$number] = $year;
                
        }
    
        // Return the field
        return $field;
        
    }
    
    add_filter('acf/load_field/key=field_0000000000000', 'sevenYearSelect');
  • Thank you for the snippet!

    Juste $number should be $year.

    $field['choices'][$number] = $year; => $field['choices'][$year] = $year;

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.