From 60fde0e0633f60419d0780b7a28de4cefb1d2b85 Mon Sep 17 00:00:00 2001 From: Sebastian Pedersen Date: Mon, 27 Oct 2025 17:33:47 -0700 Subject: [PATCH] `gppa-filter-terms-by-depth.php`: Fixed an issue where taxonomy selections were not saving in Multiple Choice fields when using checkbox input types. --- .../gppa-filter-terms-by-depth.php | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/gp-populate-anything/gppa-filter-terms-by-depth.php b/gp-populate-anything/gppa-filter-terms-by-depth.php index 8ac61f4c7..ec9c9679f 100644 --- a/gp-populate-anything/gppa-filter-terms-by-depth.php +++ b/gp-populate-anything/gppa-filter-terms-by-depth.php @@ -43,15 +43,32 @@ }; $terms = $walker( $all_terms, wp_list_pluck( $objects, 'term_id' ) ); - $choices = array(); + $filtered_choices = array(); foreach ( $terms as $object ) { - $choices[] = array( + $choice = array( 'value' => $object->term_id, 'text' => $object->name, 'object' => $object, ); + + // For Multi Choice fields with persistent choices, preserve existing choice structure. + if ( method_exists( $field, 'has_persistent_choices' ) && $field->has_persistent_choices() ) { + // Find matching choice in original choices to preserve key and other properties + foreach ( $choices as $original_choice ) { + if ( isset( $original_choice['object'] ) && $original_choice['object']->term_id == $object->term_id ) { + $choice = array_merge( $original_choice, $choice ); + break; + } + } + // If no existing choice found, generate key for new choices + if ( ! isset( $choice['key'] ) ) { + $choice['key'] = md5( $object->term_id ); + } + } + + $filtered_choices[] = $choice; } - return $choices; + return $filtered_choices; }, 10, 3 );