Index attachments and add a media type data source and facet

<?php
// See: https://facetwp.com/how-to-filter-wp-attachments-and-draft-pending-or-private-posts/#create-a-media-type-facet

// Index attachements: 'inherit' is needed for attachment indexing.
add_filter( 'facetwp_indexer_query_args', function( $args ) {
  $args['post_status'] = array( 'publish', 'inherit' );
  return $args;
});

// Add a "Media Type" option to the facet and Listing Builder data sources dropdown
add_filter( 'facetwp_facet_sources', function( $sources, $context ) {
  $sources['posts']['choices']['post_mime_type'] = 'Media Type';
  return $sources;
}, 20, 2 );
 
// Modify the indexed value for the post_mime_type. 
// Adapt as needed.
add_filter( 'facetwp_index_row', function( $params, $class ) {
  if ( 'post_mime_type' == $params['facet_source'] ) {
    
    $value = $params['facet_display_value'];
 
    $parts = explode( '/', $value ); // The full post_mime_type, e.g. 'image/png'
    $type    = $parts[0]; // e.g., 'image'
    $subtype = $parts[1]; // e.g., 'png'
    
    $params['facet_value'] = $params['facet_display_value'] = $subtype; // Index the subtype, e.g. 'png'
 
  }
  return $params;
}, 10, 2 );