WP Query with ACF repeater subfield condition

We have a CPT people, and each one people belongs to a department (which is a standard page).

In each department’s internal page we need to find the people that belong to the current department (current page, eg. $post).

First we need to add a post_where filter

add_filter( 'posts_where', function ( $where ) {
   $repeater_field_name = 'people_extra_fields';
   $sub_field_name      = 'department';

   return str_replace(
      "meta_key = '{$repeater_field_name}_%_{$sub_field_name}'",
      "meta_key LIKE '{$repeater_field_name}_%_{$sub_field_name}'",
      $where
   );
} );

then we use the following query

$repeater_field_name = 'people_extra_fields';
$sub_field_name      = 'department';
$people              = new WP_Query( [
   'post_type'      => 'people',
   'posts_per_page' => - 1,
   'meta_query'     => [
      [
         'key'     => $repeater_field_name . '_%_' . $sub_field_name,
         'value'   => $post->ID,
         'compare' => 'LIKE'
      ]
   ]
] );

ref https://support.advancedcustomfields.com/forums/topic/reverse-query-relationship-subfield-which-is-nested-in-a-repeater-field/