Multiple Dynamic Fields remove duplicates efficiently

By: Mick Follari | Asked: 03/15/2023
ForumsCategory: Code SnippetsMultiple Dynamic Fields remove duplicates efficiently
Mick Follari asked 1 year ago

Hi, I've got a form I'm using as a 'search' or filter for another form on the page. I'm populating the filter fields dynamically. I have a code snippet to try to remove duplicates, from Formidable, but it only works on one field at a time. I've tried various loop mechanisms to try to spare having to list a couple doze of these individual code snippets, but to no avail. I wonder whether anyone has any insights? This is the snippet, you can see how I am copying the same function over again, these are just the first two of many. Thisis where the snippet came from, and Formidable says they don't have a stable way to iterate it (https://formidableforms.com/knowledgebase/frm_data_sort/#kb-remove-duplicates-from-dynamic-field).

add_filter('frm_data_sort', 'frm_remove_duplicates_63', 21, 2);
function frm_remove_duplicates_63($options, $atts ) {
if ( $atts['dynamic_field']['id'] == 63) { //change 100 to the ID of the Dynamic field
$options = array_unique( $options );
}
return $options;
}
add_filter('frm_data_sort', 'frm_remove_duplicates_64', 21, 2);
function frm_remove_duplicates_64( $options, $atts ) {
if ( $atts['dynamic_field']['id'] == 64) { //change 100 to the ID of the Dynamic field
$options = array_unique( $options );
}
return $options;
}

This is an attempt to handle it in a loop but it doesn't work:

function cs_unique_dynamic_values(){
$dynamicFields = array(63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 82);
foreach ( $dynamicFields as $value ) {
add_filter('frm_data_sort', function () use ( $value, $options, $atts ) {
if ( $atts['dynamic_field']['id'] == $value) { //change 100 to the ID of the Dynamic field
$options = array_unique( $options );
}
return $options;
}, 21, 2);
}
}

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right