From e62f3ad93658c8615201d671473cf276d023e014 Mon Sep 17 00:00:00 2001 From: SebastianWiz <165194375+SebastianWiz@users.noreply.github.com> Date: Thu, 4 Dec 2025 15:09:53 -0800 Subject: [PATCH 1/2] `gppa-wpml-current-language-choices.php`: Automatically filter posts by current WPML language. --- .../gppa-wpml-current-language-choices.php | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 gp-populate-anything/gppa-wpml-current-language-choices.php diff --git a/gp-populate-anything/gppa-wpml-current-language-choices.php b/gp-populate-anything/gppa-wpml-current-language-choices.php new file mode 100644 index 000000000..a9e4444fa --- /dev/null +++ b/gp-populate-anything/gppa-wpml-current-language-choices.php @@ -0,0 +1,84 @@ +cssClass ) ? $field->cssClass : ''; + if ( strpos( $css_class, 'wpml-limit-to-current-language' ) === false ) { + return $choices; + } + + // Get current WPML language (e.g. 'nl', 'en'). + $current_lang = apply_filters( 'wpml_current_language', null ); + if ( ! $current_lang ) { + return $choices; + } + + $filtered_choices = []; + + foreach ( $choices as $index => $choice ) { + $post_id = 0; + $post_type = null; + + // Preferred: WP_Post object from $objects array. + if ( isset( $objects[ $index ] ) && $objects[ $index ] instanceof WP_Post ) { + $post_id = $objects[ $index ]->ID; + $post_type = $objects[ $index ]->post_type; + } + + // Fallback: use the choice value as post ID. + if ( ! $post_id && isset( $choice['value'] ) ) { + $post_id = absint( $choice['value'] ); + + if ( $post_id && ! $post_type ) { + $post_obj = get_post( $post_id ); + $post_type = $post_obj ? $post_obj->post_type : null; + } + } + + // If we can resolve a post and post type, apply WPML language check. + if ( $post_id && $post_type ) { + // Convert the post type to a WPML element type, e.g. 'post_post'. + $element_type = apply_filters( 'wpml_element_type', $post_type ); + + // Get the language code for this specific element. + $choice_lang = apply_filters( + 'wpml_element_language_code', + null, + [ + 'element_id' => $post_id, + 'element_type' => $element_type, + ] + ); + + // If WPML knows the language and it does not match the current language, skip this choice. + if ( $choice_lang && $choice_lang !== $current_lang ) { + continue; + } + } + + // Keep the choice. + $filtered_choices[] = $choice; + } + + return $filtered_choices; +} From 07d491421a7e024cd2903f4fa5acc1818605acb6 Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Fri, 5 Dec 2025 09:47:13 +0530 Subject: [PATCH 2/2] `gppa-wpml-current-language-choices.php`: Automatically filter posts by current WPML language. --- gp-populate-anything/gppa-wpml-current-language-choices.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gp-populate-anything/gppa-wpml-current-language-choices.php b/gp-populate-anything/gppa-wpml-current-language-choices.php index a9e4444fa..73f437911 100644 --- a/gp-populate-anything/gppa-wpml-current-language-choices.php +++ b/gp-populate-anything/gppa-wpml-current-language-choices.php @@ -33,7 +33,7 @@ function hb_wpml_limit_gppa_choices_to_current_language( $choices, $field, $obje return $choices; } - $filtered_choices = []; + $filtered_choices = array(); foreach ( $choices as $index => $choice ) { $post_id = 0; @@ -64,10 +64,10 @@ function hb_wpml_limit_gppa_choices_to_current_language( $choices, $field, $obje $choice_lang = apply_filters( 'wpml_element_language_code', null, - [ + array( 'element_id' => $post_id, 'element_type' => $element_type, - ] + ) ); // If WPML knows the language and it does not match the current language, skip this choice.