Hello @hueskedotdigital,
I hope you’re doing great today!
Here’s an example of the script for populating {select-1} field with the post titles:
<?php
add_filter(
'forminator_cform_render_fields',
function( $wrappers, $form_id ) {
$allowed_forms = array (
2268,
);
if ( ! in_array( $form_id, $allowed_forms) ) {
return $wrappers;
}
$select_fields_data = array(
'select-1' => 'post',
);
foreach ( $wrappers as $wrapper_key => $wrapper ) {
if ( ! isset( $wrapper[ 'fields' ] ) ) {
continue;
}
if (
isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) &&
! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] )
) {
$posts = get_posts( array( 'post_type' => $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] , 'numberposts' => -1 ) );
if ( ! empty( $posts ) ) {
$new_options = array();
$opt_data = array();
foreach( $posts as $post ) {
$new_options[] = array(
'label' => $post->post_title,
'value' => $post->ID,
'limit' => '',
'key' => forminator_unique_key(),
);
$opt_data['options'] = $new_options;
}
$wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] = $new_options;
$select_field = Forminator_API::get_form_field( $form_id, $wrapper[ 'fields' ][ 0 ][ 'element_id' ], true );
if ( $select_field ) {
Forminator_API::update_form_field( $form_id, $wrapper[ 'fields' ][ 0 ][ 'element_id' ], $opt_data );
}
}
}
}
return $wrappers;
},
10,
2
);
– you’ll need to replace “2268” with your form ID, and replace “select-1” with the actual field ID.
Save the code as a PHP file, for example “forminator-populate-select-field-with-posts.php“, and upload it to /wp-content/mu-plugins/ directory on the server, in order for it to run as a must use plugin.
You then can send the field value the same way, as any other fields, by using the macros in the Email Notifications section:
https://prnt.sc/-FWjg9ucX1Bb
I hope this helps. Please let us know if you have any questions.
Best Regards,
Dmytro