Remove a post from FacetWP’s index if the post_status of its ACF related post is changed

<?php

/**
 * Setup:
 * Post Type A has an ACF Relationship field related to Post Type B.
 * A facet has this relationship field set as source.
 * A related Post B can be set to draft, after which the Post A needs to be removed from FacetWP's index table for the facet.
 * FacetWP does not index draft posts, but as this is not a save or edit of Post A, this re-index will not happen automatically.
 * The following will do this:
 **/


// Don't index a Post A if the post_status of the related Post B is not 'publish':
add_filter( 'facetwp_index_row', function( $params, $class ) {
  if ( 'my_facet_name' == $params['facet_name'] ) { // Change 'my_facet_name' to name of your facet
    if ( 'publish' != get_post_status( $params[ 'facet_value' ] ) ) {
      $params[ 'facet_value' ] = ''; // Skip indexing
    }
  }
  return $params;
}, 10, 2 );

// Remove the facet choices (being related Posts B) from FacetWP's index table for Posts A if the status of a Post B is changed to something else than 'publish'.
// See: https://developer.wordpress.org/reference/hooks/transition_post_status/
add_action( 'transition_post_status', function( $new_status, $old_status, $post ) {
  if ( 'publish' != $new_status && 0 < $post->ID ) {
    global $wpdb;
    // Change 'my_facet_name' to name of your facet.
    $sql = "
            DELETE FROM {$wpdb->prefix}facetwp_index
            WHERE facet_value = $post->ID AND facet_name = 'my_facet_name'
        ";
    $wpdb->query( $sql );
  }
}, 10, 3);