Introduction

Form input is prone to errors by your users. You should try to avoid the possibility for these errors to occur, by giving users choices instead of text input fields. Gravity Forms helps you out in certain cases by offering a number of predefined lists like continents, countries, states, etc. However, sometimes you’ll need to populate these fields with information that (preferably) isn’t in a static list. In our case we wanted the titles of Custom Post Types in a select box for the use of our suggestions form.

Gravity Forms’ predefined choices

Code

Our solution requires coding. Ideally you should put your code in a plugin (we did, see our GitHub repository), but for the sake of this article we’ll use our theme’s functions.php. Before you continue: backup your site. Better safe than sorry.

Functions.php

We’ll add a new function to our functions.php that we’ll use to populate a select field in our form. Notice the $form parameter that we return immediately.

function popupate_cpt_titles( $form ) {
   return $form;
}

Form ID

Next we need to trigger the function only for the form we’re interested in. For this we need the form ID, which you can find in the ‘ID’ column in your forms overview.

Gravity Forms overview with ID column

We’re interested in the form with ID 7, so we’ll use that for the remainder of this article. To call our function, add the following filters:

add_filter( 'gform_pre_render_7', 'populate_cpt_titles' );
add_filter( 'gform_pre_validation_7', 'populate_cpt_titles' );
add_filter( 'gform_pre_submission_filter_7', 'populate_cpt_titles' );
add_filter( 'gform_admin_pre_render_7', 'populate_cpt_titles' );

Our code now looks like this:

add_filter( 'gform_pre_render_7', 'populate_cpt_titles' );
add_filter( 'gform_pre_validation_7', 'populate_cpt_titles' );
add_filter( 'gform_pre_submission_filter_7', 'populate_cpt_titles' );
add_filter( 'gform_admin_pre_render_7', 'populate_cpt_titles' );
function populate_addon_titles( $form ) {
	return $form;
}

Filter for the correct form field

We only want our function to work on a specific dropdown field. For this we need to add a class attribute to the Gravity Forms form field:

Add a class to the Gravity Forms dropdown field.

Having this class configured, we can filter for the specific select field by that class. In the code below we loop over all form fields and check for a select field with the class ‘populate_cpt_titles’:

foreach ( $form['fields'] as &$field ) {
   if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_cpt_titles' ) === false ) {
      continue;
   }
}

Placeholder

Lets check to see if the code works by setting a placeholder. We’ll do this before we start collecting values and populating the field options:

$field->placeholder = 'Select an Addon';

The full code up until now look as follows:

add_filter( 'gform_pre_render_7', 'populate_addon_titles' );
add_filter( 'gform_pre_validation_7', 'populate_addon_titles' );
add_filter( 'gform_pre_submission_filter_7', 'populate_addon_titles' );
add_filter( 'gform_admin_pre_render_7', 'populate_addon_titles' );
function populate_addon_titles( $form ) {
	foreach ( $form['fields'] as &$field ) {
		if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_addon_titles' ) === false ) {
	    	continue;
		}
		
		$field->placeholder = 'Select an Addon';
	}

	return $form;
}

The field should now look something like below in your frontend:

Select with Caption

Retrieve data and populate field

The next step is to collect the information we need. In our case we’ve got a custom post type ‘add-on’ for which we want to retrieve the title. We’ll first retrieve the data we need:

$args = [
	'posts_per_page'   => -1,
	'order'            => 'ASC',
	'orderby'          => 'post_title',
	'post_type'        => 'add-on', // Change this to your Custom Post Type
	'post_status'      => 'publish',
];
$custom_posts = get_posts( $args );

Next we’ll create an array of options for the select. We’ll loop over our Custom Post Types to populate the option array:

$options = [];
foreach( $custom_posts as $custom_post ) {
	$options[] = ['text' => $custom_post->post_title, 'value' => $custom_post->post_title];
}

The one thing left to do is actually add the retrieved titles to the select field:

$field->choices = $options;

The full code example now looks like this:

add_filter( 'gform_pre_render_7', 'populate_cpt_titles' );
add_filter( 'gform_pre_validation_7', 'populate_cpt_titles' );
add_filter( 'gform_pre_submission_filter_7', 'populate_cpt_titles' );
add_filter( 'gform_admin_pre_render_7', 'populate_cpt_titles' );
function populate_cpt_titles( $form ) {
	foreach ( $form['fields'] as &$field ) {
		if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_cpt_titles' ) === false ) {
	    		continue;
		}

		$field->placeholder = 'Select an Addon';

		$args = [
			'posts_per_page'   => -1,
			'order'            => 'ASC',
			'orderby'          => 'post_title',
			'post_type'        => 'add-on', // Change this to your Custom Post Type
			'post_status'      => 'publish',
		];
		$custom_posts = get_posts( $args );

		$options = [];
		foreach( $custom_posts as $custom_post ) {
			$options[] = ['text' => $custom_post->post_title, 'value' => $custom_post->post_title];
		}

		$field->choices = $options;
	}

	return $form;
}

Now check your frontend! It should be populated with your custom post type titles as shown in the picture below, where it show the list of our Gravity Forms add-ons:

Dropdown field with Custom Post Type titles as select options
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *