Back to Community

How to Populate Forminator Select Fields with Custom Post Data

30 threads Sep 10, 2025

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.

  1. Edit your form in the Forminator builder.
  2. Navigate to the settings for your select field.
  3. In the "Options" tab, add a single, dummy option (e.g., "-- Select --").
  4. Enable the "This is the default option" checkbox for this dummy option.
  5. 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 123 in the $allowed_forms array 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_type with 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