How to Populate a Gravity Forms Dropdown field with an Advanced Custom Fields Post Type

3 min read 11 days ago
Published on May 07, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Introduction

In this tutorial, you will learn how to populate a Gravity Forms dropdown field using a custom post type created with Advanced Custom Fields (ACF). This integration streamlines your processes by allowing dynamic content to be displayed in forms, enhancing user experience and data collection.

Step 1: Create a Custom Post Type

To begin, you need to set up a custom post type that will hold the data you want to display in your dropdown.

  1. Access your WordPress Dashboard

    • Navigate to "Custom Fields" and then to "Post Types."
  2. Add a New Post Type

    • Click on "Add New" and fill in the necessary details such as name, slug, and settings according to your needs.
  3. Publish the Custom Post Type

    • After configuring the settings, publish the post type to make it available for use.

Step 2: Add Custom Fields with ACF

Next, you will create custom fields that will be associated with your new post type.

  1. Go to Custom Fields

    • Click on “Add Field Group.”
  2. Create Fields

    • Add fields relevant to the data you want to collect. For example, if your post type is "Products," you might add fields for "Price," "Description," etc.
  3. Set Location Rules

    • Under “Location,” set the rule to show this field group if the post type is equal to your newly created post type.
  4. Publish the Field Group

    • Once all necessary fields are added, publish the field group.

Step 3: Fetch Data for the Dropdown

Now that your custom fields are set up, you need to write code to fetch this data for the Gravity Forms dropdown.

  1. Create a Custom Function

    • In your theme's functions.php file, add the following code to create a function that will pull the post titles from your custom post type:
    add_filter('gform_pre_render_YOUR_FORM_ID', 'populate_dropdown');
    add_filter('gform_pre_validation_YOUR_FORM_ID', 'populate_dropdown');
    add_filter('gform_pre_submission_filter_YOUR_FORM_ID', 'populate_dropdown');
    add_filter('gform_admin_pre_render_YOUR_FORM_ID', 'populate_dropdown');
    
    function populate_dropdown($form) {
        foreach($form['fields'] as &$field) {
            if($field->id == YOUR_FIELD_ID) {
                $field->choices = array();
                $posts = get_posts(array('post_type' => 'YOUR_CUSTOM_POST_TYPE', 'numberposts' => -1));
                foreach($posts as $post) {
                    $field->choices[] = array('text' => $post->post_title, 'value' => $post->ID);
                }
            }
        }
        return $form;
    }
    
    • Replace YOUR_FORM_ID with the ID of your Gravity Form, YOUR_FIELD_ID with the ID of the dropdown field, and YOUR_CUSTOM_POST_TYPE with the slug of your custom post type.
  2. Save Changes

    • After adding the code, make sure to save your changes.

Step 4: Test Your Form

After setting everything up, it’s time to test your form to ensure the dropdown is populated correctly.

  1. Open Your Form

    • Navigate to the page where your Gravity Form is embedded.
  2. Check the Dropdown

    • The dropdown should now display the titles of the posts from your custom post type. If it does not, double-check your code and ensure that the custom post type has published posts.
  3. Submit a Test Entry

    • Fill out the form and submit a test entry to verify that everything is working as expected.

Conclusion

You have successfully populated a Gravity Forms dropdown with data from an ACF custom post type. This integration not only enhances the functionality of your forms but also improves the user experience by providing relevant choices. For further customization, you can explore additional Gravity Forms features based on your project’s needs.