I have two gravity forms which are used on different pages of my site. Both have multi-select fields which I want to pre-populate with a list of custom post titles.
On one form, the pre-populate script works great, but on the second it doesn’t work no matter what I try. What am I missing?
Here’s the code for the filters I have on my functions page in my theme:
++++++++++++
// prepopulate gform
add_filter( ‘gform_pre_render_12’, ‘populate_posts’ );
add_filter( ‘gform_pre_validation_12’, ‘populate_posts’ );
add_filter( ‘gform_pre_submission_filter_12’, ‘populate_posts’ );
add_filter( ‘gform_admin_pre_render_12’, ‘populate_posts’ );
function populate_posts( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'multiselect' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
$args = array(
‘numberposts’ => -1,
‘posts_per_page’ => -1,
‘post_type’ => ‘na_jobs’
);
$posts = get_posts( $args );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a Job ID#';
$field->choices = $choices;
}
return $form;
}
// prepopulate gform
add_filter( ‘gform_pre_render_13’, ‘populate_posts’ );
add_filter( ‘gform_pre_validation_13’, ‘populate_posts’ );
add_filter( ‘gform_pre_submission_filter_13’, ‘populate_posts’ );
add_filter( ‘gform_admin_pre_render_13’, ‘populate_posts’ );
function populate_posts_applicants( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'multiselect' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
$args = array(
‘numberposts’ => -1,
‘posts_per_page’ => -1,
‘post_type’ => ‘applicants’
);
$posts = get_posts( $args );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select an Applicant ID#';
$field->choices = $choices;
}
return $form;
}
++++++++++++
For the moment I disabled the multiselect on this form until I can figure out the issue.