Skip to content

Commit 668f586

Browse files
authored
gppa-filter-terms-by-depth.php: Fixed an issue where taxonomy selections were not saving in Multiple Choice fields when using checkbox input types.
1 parent 2c9ef1f commit 668f586

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

gp-populate-anything/gppa-filter-terms-by-depth.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,32 @@
4343
};
4444

4545
$terms = $walker( $all_terms, wp_list_pluck( $objects, 'term_id' ) );
46-
$choices = array();
46+
$filtered_choices = array();
4747

4848
foreach ( $terms as $object ) {
49-
$choices[] = array(
49+
$choice = array(
5050
'value' => $object->term_id,
5151
'text' => $object->name,
5252
'object' => $object,
5353
);
54+
55+
// For Multi Choice fields with persistent choices, preserve existing choice structure.
56+
if ( method_exists( $field, 'has_persistent_choices' ) && $field->has_persistent_choices() ) {
57+
// Find matching choice in original choices to preserve key and other properties
58+
foreach ( $choices as $original_choice ) {
59+
if ( isset( $original_choice['object'] ) && $original_choice['object']->term_id == $object->term_id ) {
60+
$choice = array_merge( $original_choice, $choice );
61+
break;
62+
}
63+
}
64+
// If no existing choice found, generate key for new choices
65+
if ( ! isset( $choice['key'] ) ) {
66+
$choice['key'] = md5( $object->term_id );
67+
}
68+
}
69+
70+
$filtered_choices[] = $choice;
5471
}
5572

56-
return $choices;
73+
return $filtered_choices;
5774
}, 10, 3 );

0 commit comments

Comments
 (0)