I have custom code which sets a select field for a new Grant Application post. The code is shown below. This is working as I can see the value is set in the database.
add_filter( 'wpt_field_options', 'sc_populate_grantyear_field', 10, 2 );
function sc_populate_grantyear_field( $current_options, $title_of_field ){
if ('Grant Year' == $title_of_field ) {
$selectYear = get_option('sc-grantyear');
$current_options = array();
$current_options[] = array(
'#title' => $selectYear,
'#value' => $selectYear
);
}
return $current_options;
}
The problem comes when I try and display the grant-year-select field, which has the title "Grant Year", using the types shortcode. It is only displaying the value if the value matches the sc-grantyear. So if the sc-grantyear is set to "2019" then the following will only display a value if the value is 2019.
[types field='grant-year-select'][/types]
If I disable the function sc_populate_grantyear and then view existing posts the shortcode will display the grant-year-select correctly. So this is showing me the value is saved to the post correctly. But for some reason my function is preventing the value from being displayed unless it matches sc-grantyear.
Hi Maureen,
Thank you for contacting us and I'll be happy to assist.
The code that you've shared, bypasses any options that you've set in "Grant Year" select field's settings and any values that may be saved in the field previously. It will only show what is saved in "sc-grantyear" option.
This means that whenever you'll be adding or editing the post from the admin area or through the front end form, the "Grant Year" field will only show one select option, based on the year value set in the "sc-grantyear" option.
If you could share some more details around what you're planning to achieve, I'll be in a better position to guide you accordingly.
regards,
Waqar
Yes, that is what we want. The grant year select field is hidden and set automatically using this code based on another variable. Each year applicants can apply for a grant. We set the grant year according to the funding year. So the post type Grant Applications has a number of posts each reflecting the grant year in which they were submitted for a grant.
Where I'm having a problem is displaying the value of that field on the front end using Types. Regardless of what the stored value is in the grant year select field the types shortcode is returning the value of the sc-grantyear variable. I can't figure out why. When I look at the database I see the correct value in the grant year select field. Is there additional information I can provide you to help with this issue?
Hi Maureen,
The value that is returned through the "[types field='grant-year-select'][/types]" shortcode is filtered through the function attached to "wpt_field_options".
But, if you'll use View's "[wpv-post-field name="wpcf-grant-year-select"]" shortcode, it will return what is saved in the database, as it is.
( ref: https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-field )
I hope this helps!
regards,
Waqar
Thank you for helping to solve the mystery. I had not expected the Types shortcode to filter through wpt_field_options. Using [wpv-post-field name="wpcf-grant-year-select"] worked perfectly. Thanks for your assistance.
My issue is resolved now. Thank you!