Hi Chris,
Thanks for responding. I know I was probably being idiotic but I just repeated the code snippet with different names and variables. See below…
Snippet 1:
add_filter( 'gform_advancedpostcreation_post_after_creation_4', 'apc_serialize_checkboxes_2', 10, 4 );
function apc_serialize_checkboxes_2( $post_id, $feed, $entry, $form ) {
// Checkboxes field id.
$field_id = 37;
$form = 4;
// Get field object.
$field = GFAPI::get_field( $form, $field_id );
if ( $field->type == 'checkbox' ) {
// Get a comma separated list of checkboxes checked
$checked = $field->get_value_export( $entry );
// Convert to array.
$values = explode( ', ', $checked );
}
// Replace my_custom_field_key with your custom field meta key.
update_post_meta( $post_id, 'education_level', $values );
}
Snippet 2
add_filter( 'gform_advancedpostcreation_post_after_creation_5', 'apc_serialize_checkboxes_3', 10, 4 );
function apc_serialize_checkboxes_3( $post_id, $feed, $entry, $form ) {
// Checkboxes field id.
$field_id_2 = 24;
$form = 4;
// Get field object.
$field_2 = GFAPI::get_field( $form, $field_id_2 );
if ( $field_2->type == 'checkbox' ) {
// Get a comma separated list of checkboxes checked
$checked_2 = $field_2->get_value_export( $entry );
// Convert to array.
$values_2 = explode( ', ', $checked2 );
}
// Replace my_custom_field_key with your custom field meta key.
update_post_meta( $post_id, 'interests', $values_2 );
}
The second snippet doesn’t work.
And I also made an attempt to combine the functions and update the post meta with an array but this is well outside my PHP comfort zone - but I suspect this is the proper and scalable way to do this…
Snippet 3
add_filter( 'gform_advancedpostcreation_post_after_creation_5', 'apc_serialize_checkboxes_2', 10, 4 );
function apc_serialize_checkboxes_2( $post_id, $feed, $entry, $form ) {
// Checkboxes field id.
$field_id = 24;
$field_2_id = 27;
$form = 4;
// Get field object.
$field = GFAPI::get_field( $form, $field_id );
$field_2 = GFAPI::get_field( $form, $field_2_id );
if ( $field->type == 'checkbox' ) {
// Get a comma separated list of checkboxes checked
$checked = $field->get_value_export( $entry );
$checked_2 = $field_2->get_value_export( $entry );
// Convert to array.
$values = explode( ', ', $checked );
$values_2 = explode( ', ', $checked_2 );
}
$metaValues = array(
'interests' => '$values',
'education_level' => '$values_2',
);
// Set all key/value pairs in $metaValues
foreach ($metaValues as $metaKey => $metaValue) {
update_post_meta($postId, $metaKey, $metaValue);
}
Any pointers suggestions would be much appreciated.
Thanks,
Mat