Last active
August 16, 2019 14:10
-
-
Save jaredatch/e397d6c73999ce4956d759511def5b32 to your computer and use it in GitHub Desktop.
WPForms populate select field from custom post type posts
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 | |
| /** | |
| * WPForms populate select field from custom post type posts | |
| * | |
| * @param array $field | |
| * @param array $field_atts | |
| * @param array $form_data | |
| * @return array | |
| */ | |
| function wpf_custom_select( $field, $field_atts, $form_data ){ | |
| // Only run for field #4 in Form #1 | |
| if ( $form_data['id'] == 1 && $field['id'] == '4' ) { | |
| $field['choices'] = array(); | |
| // Get all the custom post time posts | |
| $hotels = get_posts( array( 'post_type' => 'hotel', 'posts_per_page' => -1 ) ); | |
| // Loop through and add each one to the select choices | |
| foreach( $hotels as $hotel ) { | |
| $field['choices'][] = array( 'label' => $hotel->post_title ); | |
| } | |
| } | |
| return $field; | |
| } | |
| add_filter( 'wpforms_display_field_select', 'wpf_custom_select', 10, 3 ); |
And the filter seems to be wpforms_display_field_select instead.
Author
Thanks, updated :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's a missing semi-colon on line 26.