Welcome Guest! Login? Checkout
This post was written 2 years ago and therefore may not be as accurate as more recent posts.

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

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:-

check-dropdown

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.

 
 
 

Comments

Comments are closed.