Hi,
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.
Thanks in advance,
Kostas
Hi Kostas,
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.
Hi waqar.a,
Thank you for responding, your suggested code works fine!
However, according to this:
https://toolset.com/forums/topic/title-in-hook-wpt_field_options-not-working-with-term-field/
wpt_field_options filter is undocumented and not officially supported.
Does this mean that it can stop working anytime in the future?
Thanks again,
Kostas
Hi Kostas,
Thank for the update.
Glad to know that solution I shared helped you to resolve your issue.
We do have plans to document that filter and we do have an internal ticket for that. Feel free to use that filter, it should work without any issue.
Excellent, thank you waqar.a!
Best Regards,
Kostas