Home › Forums › General Issues › How to auto-populate WPForms 'Dynamic Post Type Source' dropdown with a list of
I have ACF Fields setup for Post Type while using WooCommerce. Each single Product has unique ACF Fields such as {“location”} value and {“brochure”} value.
I would like to use WPForms’Dynamic Post Type Source’ to create a dropdown list that autopopulates dynamic choices based on the values of these ACF Fields, And ideally only for Products that are In-Stock to reduce size of dropdown list.
DROPDOWN LIST:
Location 1 – Brochure URL
Location 2 – Brochure URL
Location 3 – Brochure URL
Location 4 – Brochure URL
etc
I understand within Advanced settings, Dynamic Choices can be selected for Post Type or Taxomny, but I’m not sure how this can be achieved with ACF Field Values.
The only workaround I have found so far is by adding custom WordPress taxonomy that is only related to products. It would act and show just like categories and tags do and show up on the Products page, which would allow the ability to select it from the Dynamic Choices inside WPForms. However, the major limitation here is that I would need to add the taxonomy to every single existing product, and it doesn’t consider stock status.
function add_custom_taxonomies() {
// Add new "Locations" taxonomy to Posts
register_taxonomy('location', 'product', array(
// Hierarchical taxonomy (like categories)
'hierarchical' => true,
// This array of options controls the labels displayed in the WordPress Admin UI
'labels' => array(
'name' => _x( 'Locations', 'taxonomy general name' ),
'singular_name' => _x( 'Location', 'taxonomy singular name' ),
'search_items' => __( 'Search Locations' ),
'all_items' => __( 'All Locations' ),
'parent_item' => __( 'Parent Location' ),
'parent_item_colon' => __( 'Parent Location:' ),
'edit_item' => __( 'Edit Location' ),
'update_item' => __( 'Update Location' ),
'add_new_item' => __( 'Add New Location' ),
'new_item_name' => __( 'New Location Name' ),
'menu_name' => __( 'Locations' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'locations', // This controls the base slug that will display before each term
'with_front' => false, // Don't display the category base before "/locations/"
'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
),
));
}
add_action( 'init', 'add_custom_taxonomies', 0 );
It has been a very long time since I’ve done this and a better source of information would be to contact support over at WPForms, they are generally very helpful.
But, I did once upon a time create do dynamic generation of field choices for a WPForms field. It involved using this hook
$filtered_field = apply_filters( "wpforms_{$field['type']}_field_display", $field, $attributes, $form_data );
You can supply a filter for each field type. Here is a list of all the filters available for WPForms.
I can tell you that you need to check the form ID $form_data['id']
and the field ID $field['id']
to make sure that you are altering the correct field.
Just like acf there is $field['choices']
and each choice is an array
array(
'label' => 'XXXXX',
'value' => 'YYYYY'
)
and that there may be other indexes depending on the field type.
That is a about the extent of what I can offer.
I did use a lot of this when I was building the filter and just like ACF it gave me information about the field I was working on at the time.
echo '<pre>'; print_r($field); echo '</pre>';
Thank you John, this is first class excellent information. Very helpful Thanks.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.