-
-
Save infocities/87ef6449db4bca5de2541916ba24d483 to your computer and use it in GitHub Desktop.
Show Custom Post Types in Gravity Form Select Field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* Show Custom Post Types in Gravity Form Select Field */ | |
| add_filter( 'gform_pre_render_2', 'populate_cpt_titles' ); | |
| add_filter( 'gform_pre_validation_2', 'populate_cpt_titles' ); | |
| add_filter( 'gform_pre_submission_filter_2', 'populate_cpt_titles' ); | |
| add_filter( 'gform_admin_pre_render_2', 'populate_cpt_titles' ); | |
| function populate_cpt_titles( $form ) { | |
| foreach ( $form['fields'] as &$field ) { | |
| if ( $field->type != 'select' || strpos( $field->cssClass, 'populate_cpt_titles' ) === false ) { | |
| continue; | |
| } | |
| $field->placeholder = 'Select Service'; | |
| $args = [ | |
| 'posts_per_page' => -1, | |
| 'order' => 'ASC', | |
| 'orderby' => 'post_title', | |
| 'post_type' => 'service', // Change this to your Custom Post Type | |
| 'post_status' => 'publish', | |
| ]; | |
| $custom_posts = get_posts( $args ); | |
| $options = []; | |
| foreach( $custom_posts as $custom_post ) { | |
| $options[] = ['text' => $custom_post->post_title, 'value' => $custom_post->post_title]; | |
| } | |
| $field->choices = $options; | |
| } | |
| return $form; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment