I have created a new post type with a new field "production-year", as a required select field, with 2018, 2017, 2016 as options. Also I have built a cred form to add new content. In this form, I need to programmaticaly build the select year options (current year and two previous years) with a php snippet (I am aware of the php code to use).
What is the recommended way to tackle this? Please provide any guidance.
Thank you for contacting us and I'll be happy to assist.
To programmatically generate select field options (into admin area custom fields as well as on the front end through CRED form), you can use “wpt_field_options” filter: e.g.
add_filter( 'wpt_field_options', 'func_to_dynamically_populate_production_year', 10, 3);
function func_to_dynamically_populate_production_year( $options, $title, $type ){
switch( $title ){
case 'Production Year':
$options = array();
// add or generate the array of your desired option titles and values
$options[] = array('#value' => '', '#title' => '---',);
break;
}
return $options;
}
Note: Please make sure to update ‘Production Year’ with the actual “Field name” (not slug) of your custom field
I hope this helps! Please let us know if you need any further assistance.