Welcome Guest! Login? Checkout

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.

Further Reading

You may be interested in my post on using Gravity Forms & Custom Post Types together.

 
 
 
WP Engine Managed WordPress Hosting

Comments

Polite Disclaimer: I am welcome, open and willing for corrections to be shared in the comments (with corrections being added to posts and credited), and the comments field should be used to promote discussion and make this post better. I do not know everything and if anybody finds a better way to do something, then by all means please share it below.

However, I'm unable to offer support to posts. The reason being is that WordPress has tens of thousands of plugins and millions of themes. As such, finding out exactly why code doesn't work with your setup is a long process. If you wish for me to look at your code, please use the priority support area.

Comments asking support will be left on the site, but there is no guarantee of answer.

  • Tony

    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

Comments are closed.