Facetwp index a Pods relationship field


/** Index the post meta of a custom post field of a related post (from a relationship field) in the Pods plugin
 ** Your facet needs to have a Pods (bidirectional) relationship field set as data source
 ** $related_post_id can be used with get_post_meta() or other functions to get info about the related post
 ** Make sure to re-index after adding this code

add_filter( 'facetwp_index_row', function( $params, $class ) {
  if ( 'my_facet_name' == $params['facet_name'] ) { // Change 'my_facet_name' to the name of your facet that has the (bidirectional) relationship field as data source.
    $related_post_id = $params['facet_value']; // Depending on how your relationship field saves its data this might need to be changed or looked up from a different value.
    if ( ! empty( $related_post_id ) ) {
      $params['facet_display_value'] = get_post_meta( $related_post_id, 'field_name', true ); // Lookup the custom field. Change 'field_name' to your custom field name. This would need adjusting if it returns an array rather than text string.
      $params['facet_value'] = $params['facet_display_value'];
    } else {
      $params['facet_value'] = ''; // Don't index this row.

  return $params;
}, 10, 2 );