Pre-populate multi select from ACF data
-
Hi there,
I have a custom post type with ACF custom fields, I want to show this custom fields as a multi select in the form. I already have the post ID, but I am not sure how to do it programmatically. Please help.Thanks!
-
Hi @satrya,
I hope you are doing good today.
Could you please confirm if you want the custom post fields created using Advanced Custom Fields as options in a multi-select field?
I am afraid this is not possible out of the box. If you could briefly explain what exactly you are trying to do, we can check with our developers to see if they can suggest a quick workaround.
Kind Regards,
Nebu JohnSo, in this page , if you scroll a bit, see the Accessories section on the sidebar, if you click it, it will show the options. This data is come from ACF custom fields. I want to show the same data on the form page, this button will bring the user to the form page, but I have not added the URL to the form yet.
So, the URL will be like this
https://website.com/apply-loan/?car_id=12345. The button URL will bring thepost_id, and my plan is to use thisidto display the ACF custom fields data, so the user can change the accessories they want on the form page.By the way, the data will be in multi-select.
-
This reply was modified 3 years, 10 months ago by
satrya.
Hi @satrya
Thank you for the information.
In this case, you can use the Query Parameter option https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#pre-populate-form-field-values, if it is multiple select you can use car_id=1,2,3 Or in case you mean, multiple fields selected you can use car_id=1&another_option=3 and build your URL.
https://monosnap.com/file/g7BeYUhjucXj047Fgpt0rDf4C23E83
Best Regards
Patrick FreitasOh I think you missed understood my question. If you see your image, there is a select box with options (Option 1, Option 2, etc). I’d like to replace that, the options (Option 1, Option 2, etc) with the same data as the Accessories option on car single page. https://monosnap.com/file/VI71iAOygJKapTaJ8yR4A9Y7ajxGvU
Is there a way to do this programmatically?
Inject the custom fields data to the select box?
Hi @satrya
Option 1, and Option 2 is only an example from my end,
You would need to create your form and list the options, then using the Query Parameter you can select specific options of your form as the screenshot is showing unless you are looking to create the options on the fly for each car, but in this case, I am afraid is not possible from the plugin interface, and would require some custom coding.
This is an example of code: https://gist.github.com/patrickfreitasdev/d7ad926561db4b598baeaf66478d790b
It uses PHP to create the Select based on Post titles, you would need to use similar code to create based on an ACF field.
Best Regards
Patrick FreitasHi @satrya
Looking further your question, can you let us know if all cars share the same options or it would have different options for each car?
Best Regards
Patrick FreitasHi Patrick,
Thank you for the answer, the gist you shared, I think it can be work. About your last question, the options will be different, that’s why I bring the car ID to the form, so I can pull the custom fields data based on ID.Hi @satrya
Thank you for the confirmation.
In that case, you would need to use the forminator_replace_form_data & forminator_cform_render_fields filters indeed to generate the options dynamically, if you get stuck on any coding part feel free to let us know.
Best Regards
Patrick FreitasBoth filters are working perfectly, but I have another question, is there a way to customize the input checkbox, please see this image, I’d like to add
data-price=""to the markup.Hi @satrya,
Forminator does not have an option to add an attribute to an input field out of the box. You may please try the following jQuery.
$('#someid').attr('name', 'value');I hope that helps.
Kind Regards,
Nebu JohnHi Nebu John,
So, there is no action or filter to customize the multi select markup?Hi @satrya
There’s no filter that would directly apply to field “attributes” like this. The two ways to deal with it are either to use custom JS code or to do a PHP string replacement or regex replacement on entire field markup.
In case of checkbox-type fields the filter used in plugin code is:
apply_filters( 'forminator_field_multiple_markup', $html, $id, $required, $options, $value_type );so you’d basically need to do replace on $html string.
Best regards,
AdamThanks Adam, but for now the easiest for me is just to use the
calculation, I am using it to store custom value to the field. -
This reply was modified 3 years, 10 months ago by
The topic ‘Pre-populate multi select from ACF data’ is closed to new replies.