Skip to content

Instantly share code, notes, and snippets.

@jaredatch
Last active August 16, 2019 14:10
Show Gist options
  • Select an option

  • Save jaredatch/e397d6c73999ce4956d759511def5b32 to your computer and use it in GitHub Desktop.

Select an option

Save jaredatch/e397d6c73999ce4956d759511def5b32 to your computer and use it in GitHub Desktop.
WPForms populate select field from custom post type posts
<?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 );
@sanzeeb3
Copy link

There's a missing semi-colon on line 26.

@sanzeeb3
Copy link

And the filter seems to be wpforms_display_field_select instead.

@jaredatch
Copy link
Author

Thanks, updated :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment