Listing Builder Dev mode query to get posts for which a custom field in a related post is false

<?php

// Add this to the Listing Builder listing's Query tab in Dev mode
// Situation: 'enti' post type posts that have an ACF Relationshipship field that relates them with 'progetti' posts
// The 'progetti' posts have a custom field 'disattiva_dol' which is an ACF true/false field.
// The generated query arguments retrieve 'enti' post type posts
// that have a related 'progetti' post for which the 'disattiva_dol' custom field is false

// Step 1: Get the IDs of "project" posts where disattiva_dol is false.
$valid_project_ids_args = array(
  'post_type'      => 'progetti',
  'posts_per_page' => -1, // Retrieve all matching projects
  'fields'         => 'ids', // Return only post IDs for efficiency
  'meta_query'     => array(
    array(
      'key'     => 'disattiva_dol', // The ACF true/false field on 'project' posts
      'value'   => 0,               // ACF stores 'false' as 0 (for true/false fields)
      'compare' => '=',
      'type'    => 'NUMERIC',       // Ensure numeric comparison
    ),
  ),
);

$valid_project_ids = get_posts( $valid_project_ids_args );

// Step 2: Create the WP_Query arguments for "enti" posts based on the valid_project_ids.
$enti_query_args = array(
  'post_type'      => 'enti',
  'posts_per_page' => -1, // Or your desired number of posts
  'post_status'    => 'publish', // Only published 'enti' posts
);

if ( ! empty( $valid_project_ids ) ) {
  $project_relationship_meta_query = array(
    'relation' => 'OR', // An 'enti' post can be related to ANY of the valid projects
  );

  foreach ( $valid_project_ids as $project_id ) {
    // Relationship field stores a single ID (numeric comparison)
    $project_relationship_meta_query[] = array(
      'key'     => 'progetti_oggetto', // Replace with the actual name of your ACF Relationship field on 'enti' posts
      'value'   => $project_id,
      'compare' => '=',
      'type'    => 'NUMERIC',
    );

  }

  $enti_query_args['meta_query'] = $project_relationship_meta_query;

} 

// For checking the resulting args, remove when it works
// echo '<pre>'; var_dump($enti_query_args); echo '</pre>';

return $enti_query_args;