webmgt
(webmgt)
April 27, 2020, 8:22am
1
Hi, I’m trying to use this GF docs as an example to populate a drop down of my form to show everything under my custom post type and a custom field & selected value.
However I’ve tried it I seem to get nothing coming through, can anyone help with some suggestions or advice please
add_filter( 'gform_pre_render_8', 'populate_free_courses' );
add_filter( 'gform_pre_validation_8', 'populate_free_courses' );
add_filter( 'gform_pre_submission_filter_8', 'populate_free_courses' );
add_filter( 'gform_admin_pre_render_8', 'populate_free_courses' );
function populate_free_courses( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-free-courses' ) === false ) {
continue;
}
$posts = array(
'post_type' => 'free_courses',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'web_enrolments_system',
'value' => 'yes',
)
)
);
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array(
'order' => 'ASC',
'orderby' => 'title',
'text' => $post->post_title,
'value' => get_permalink($post->ID)
);
}
$field->placeholder = 'Select a course';
$field->choices = $choices;
}
return $form;
}
webmgt
(webmgt)
April 27, 2020, 8:31am
2
I may of solved this always the same… here is what I came up with
add_filter( 'gform_pre_render_8', 'populate_free_courses' );
add_filter( 'gform_pre_validation_8', 'populate_free_courses' );
add_filter( 'gform_pre_submission_filter_8', 'populate_free_courses' );
add_filter( 'gform_admin_pre_render_8', 'populate_free_courses' );
function populate_free_courses( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-free-courses' ) === false ) {
continue;
}
$freeposts = array(
'post_type' => 'free_courses',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'web_enrolments_system',
'value' => 'yes',
)
)
);
$posts = get_posts( $freeposts );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array(
'order' => 'ASC',
'orderby' => 'title',
'text' => $post->post_title,
'value' => get_permalink($post->ID)
);
}
$field->placeholder = 'Select a course';
$field->choices = $choices;
}
return $form;
}
chrishajer
(Chris (Gravity Forms))
April 27, 2020, 1:58pm
3
Is that working for you @webmgt ?
webmgt
(webmgt)
April 27, 2020, 2:28pm
4
@chrishajer seems to be, unless you have any other suggestions on how I can improve it
chrishajer
(Chris (Gravity Forms))
April 27, 2020, 7:45pm
5
So long as it works, the approach looks OK to me.
david
(David (Gravity Wiz))
May 6, 2020, 7:57pm
6
Great solution, @webmgt !
If you ever need to do this again and don’t want to touch code, Populate Anything can handle this like it ain’t even a thing.
Dynamically filter and populate field choices and values with posts, users, taxonomies, terms, Gravity Forms entries, and databases. Pretty much anything!
Here’s an example of what your custom code would look like if configured in Populate Anything: