Recently, we’ve been using Gravity Forms on some pretty complex forms. One of the most complex forms had a bunch of select fields, and one of them had to list all posts associated with a custom post type.
Gravity Forms has in their documentation a check on a foreach loop where we check the field type is equal to a value (such as input, checkbox, radio, etc.). This is done using checking the ['inputType']
index in the $field
array. Whilst suitable for forms with only one field, this could lead to all select fields in the form to have the same value. We need to target only one field.
You could check the ['id']
of the $field
array, which would allow you target individual entries in the form. However the problem with this was the client was pretty savvy, and was after a way of prepopulating other forms using similar code.
In the end, we decided to check whether a select field had a CSS class (in our case, populate-with-posts
). If it does have said class, then we populate the select options with all posts.
The Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /** * Populate a Gravity Form drop down with posts * * @link https://www.winwar.co.uk/2015/07/populate-a-gravity-forms-drop-down-field-with-a-custom-post-type/?utm_source=codesnippet * * @param object $form The form object * @return object The form object */ function winwar_populate_dropdown_with_posts($form){ foreach($form['fields'] as &$field){ if($field['inputType'] != 'select' || strpos($field['cssClass'], 'populate-with-posts') === false) continue; $posts = get_posts('numberposts=-1&post_status=publish&post_type=post'); // update 'Select a Post' to whatever you'd like the instructive option to be $choices = array(array('text' => 'Select a Post', 'value' => ' ')); foreach($posts as $post){ $choices[] = array('text' => $post->post_title, 'value' => $post->ID); } $field['choices'] = $choices; } return $form; } add_filter('gform_pre_render', 'winwar_populate_dropdown_with_posts'); |
Implementation
To implement this, all you need to do is on any post add the populate-with-posts class in the appearance tab like so:-
From there, your drop down will be populated with your posts.
Further Improvement
This was only a very basic implementation, it could be improved. For example, you can possibly use it to check all post types and then populate it with those posts (so if you have a post type of events, using the CSS check for populate-with-events. It shouldn’t be too tricky to implement. I may revisit this one day and implement it further.
Further Reading
You may be interested in my post on using Gravity Forms & Custom Post Types together.
Great, thanks for sharing i will try and use this in my current project.
However i had to alter some code,
if($field[‘inputType’] != ‘select’ || strpos($field[‘cssClass’], ‘populate-with-posts’) === false)
to this,
if($field[‘inputType’] == ‘select’ && strpos($field[‘cssClass’], ‘populate-with-posts’) === true)
/cheers
August 21, 2015 at 1:22 pm