How to Populate Forminator Select Fields with Custom Post Data
Content
Many WordPress site builders use Forminator to create powerful front-end forms. A common and advanced use case is dynamically populating a dropdown (select) field with titles from a Custom Post Type (CPT). This is incredibly useful for creating relationships between form submissions and existing content, such as letting a user choose a product, event, or project.
While Forminator doesn't include this as a built-in feature, it is possible to achieve with a custom code snippet. This guide will walk you through the process and address a common validation error that can occur.
The Problem: Static vs. Dynamic Select Fields
By default, you manually enter the options for a Select field in Forminator's form builder. This is fine for static lists that never change, like "Salutation (Mr./Mrs./Ms.)". However, for a list that changes often, like the titles of your blog posts or custom posts, manually updating the form every time you publish or edit something is impractical and error-prone.
The goal is to have the select field automatically query your database and populate itself with the current list of CPT titles every time the form loads.
The Solution: A Custom PHP Snippet
The following code snippet, often suggested by the Forminator team in support threads, uses the forminator_cform_render_fields filter hook to dynamically populate a select field. Here is a commented and explained version of the code.
add_filter(
'forminator_cform_render_fields',
function( $wrappers, $form_id ) {
// 1. Define the Form ID(s) where this should run
$allowed_forms = array (
123, // Replace 123 with your actual Form ID
);
// Only run this code on the forms specified in the array above
if ( ! in_array( $form_id, $allowed_forms) ) {
return $wrappers;
}
// 2. Query your Custom Post Type
$posts = get_posts( array(
'post_type' => 'your_custom_post_type', // Replace with your CPT slug
'post_status' => 'publish',
'numberposts' => -1, // Get all posts
'order' => 'ASC',
'orderby' => 'title'
) );
// 3. Prepare the data for the select field
$select_fields_data = array();
foreach ( $posts as $post ) {
// Set the post title as both the label and the value
$select_fields_data[] = array(
'label' => $post->post_title,
'value' => $post->post_title,
);
}
// 4. Locate and update the target select field
foreach ( $wrappers as $key => $wrapper ) {
if ( ! isset( $wrapper['fields'] ) ) {
continue;
}
foreach ( $wrapper['fields'] as $f => $field ) {
// Target the field with the slug 'select-1'
if ( false !== strpos( $field['element_id'], 'select-1' ) ) {
// Overwrite the existing 'options' array with our dynamic one
$wrappers[$key]['fields'][$f]['options'] = $select_fields_data;
}
}
}
return $wrappers;
},
10,
2
);
Resolving the "Selected value does not exist" Error
A frequent issue after implementing this code is that form submission fails with a validation error: "Selected value does not exist." This happens because Forminator's server-side validation checks the submitted value against the options that were originally configured in the form builder, not the options dynamically injected by our code.
Solution: To bypass this validation, you must add all possible dynamic values to the select field in the form builder. This seems counterintuitive but is necessary.
- Edit your form in the Forminator builder.
- Navigate to the settings for your select field.
- In the "Options" tab, add a single, dummy option (e.g., "-- Select --").
- Enable the "This is the default option" checkbox for this dummy option.
- Save the form.
This dummy option tricks the validation into passing, as the field is no longer technically empty. The dynamic code will completely replace this dummy option when the form is rendered on the front end, but the validation step will be satisfied upon submission.
Important Considerations
- Testing: Always test custom code on a staging or development site before deploying it to your live website.
- Form ID: You must replace
123in the$allowed_formsarray with your actual Forminator form ID. You can find this ID in the shortcode used to embed the form:[forminator_form id="123"]. - Post Type Slug: Replace
your_custom_post_typewith the exact slug of your CPT (e.g.,projects,events). - Beyond Basic Support: Creating and troubleshooting custom code of this nature is generally outside the scope of free support for the Forminator plugin. For complex modifications, consider hiring a WordPress developer.
This method provides a robust way to create dynamic, data-driven forms that seamlessly integrate with your site's content.
Related Support Threads Support
-
Post submission custom taxonomy won’t be appliedhttps://wordpress.org/support/topic/post-submission-custom-taxonomy-wont-be-applied/
-
trying to create a create a post form for eventshttps://wordpress.org/support/topic/trying-to-create-a-create-a-post-form-for-events/
-
ACF Field Mappinghttps://wordpress.org/support/topic/acf-field-mapping/
-
Field Values are not updating in wordpresshttps://wordpress.org/support/topic/field-values-are-not-updating-in-wordpress/
-
Fill in Select-Field with Custom Post Datahttps://wordpress.org/support/topic/fill-in-select-field-with-custom-post-data/
-
Combining fields to generate post titlehttps://wordpress.org/support/topic/combining-fields-to-generate-post-title/
-
Image array upload > ACF not workinghttps://wordpress.org/support/topic/image-array-upload-acf-not-working/
-
Customized registration form with post creation – recently stopped workinghttps://wordpress.org/support/topic/customized-registration-form-with-post-creation-recently-stopped-working/
-
Post Data max word counthttps://wordpress.org/support/topic/post-data-max-word-count/
-
Post excerpt and categoryhttps://wordpress.org/support/topic/post-excerpt-and-category/
-
Create a custom post based on form datahttps://wordpress.org/support/topic/create-a-custom-post-based-on-form-data/
-
List of posts in the select fieldhttps://wordpress.org/support/topic/list-of-posts-in-the-select-field/
-
Plain text form fieldhttps://wordpress.org/support/topic/plain-text-form-field/
-
Default Value for Post Datahttps://wordpress.org/support/topic/default-value-for-post-data/
-
Allow Users To Add Code Blocks to Post Submissionshttps://wordpress.org/support/topic/allow-users-to-add-code-blocks-to-post-submissions/
-
Star Ratings Not Appearing in Posthttps://wordpress.org/support/topic/star-ratings-not-appearing-in-post/
-
Populating Select Field with CPT Titleshttps://wordpress.org/support/topic/populating-select-field-with-cpt-titles/
-
Can I assign post slug in create post form?https://wordpress.org/support/topic/can-i-assign-post-slug-in-create-post-form/
-
Custom Post Type Submission Page Templatehttps://wordpress.org/support/topic/custom-post-type-submission-page-template/
-
Publish Post with File Download linkhttps://wordpress.org/support/topic/publish-post-with-file-download-link/
-
Custom post types & Taxonomyhttps://wordpress.org/support/topic/custom-post-types-taxonomy-3/
-
Remove Forminator post meta boxhttps://wordpress.org/support/topic/remove-forminator-post-meta-box/
-
Make the uploaded image from form the featured image of posthttps://wordpress.org/support/topic/make-the-uploaded-image-from-form-the-featured-image-of-post/
-
Extract Uploaded File’s Base Name and Output Using Post Custom Datahttps://wordpress.org/support/topic/extract-uploaded-files-base-name-and-output-using-post-custom-data/
-
Automatically Add File URL to Post after Submitting Formhttps://wordpress.org/support/topic/automatically-add-file-url-to-post-after-submitting-form/
-
Issue with Creating Custom Post After User Registration Using Forminator Formhttps://wordpress.org/support/topic/issue-with-creating-custom-post-after-user-registration-using-forminator-form/
-
Adding post title as check boxhttps://wordpress.org/support/topic/adding-post-title-as-check-box/
-
Post data Form not workinghttps://wordpress.org/support/topic/post-data-not-working/
-
Forminator form to create posts with post data fields not showing up in postshttps://wordpress.org/support/topic/forminator-form-to-create-posts-with-post-data-fields-not-showing-up-in-posts/
-
Trying to hide Submit button using Post Data fieldhttps://wordpress.org/support/topic/trying-to-hide-submit-button-using-post-data-field/