facetwp index attachments and add a media type data source

djrmom

{{gist file=”custom-hooks.php” lang=”php”}}
<?php
/** inherit adds attachment indexing **/
add_filter( ‘facetwp_indexer_query_args’, function( $args ) {
$args[‘post_status’] = array( ‘publish’, ‘inherit’ );
return $args;
});

/** post_mime_type is the column in the posts table to add **/
add_filter( ‘facetwp_facet_sources’, function( $sources ) {
$sources[‘posts’][‘choices’][‘post_mime_type’] = ‘Media Type’;
return $sources;
});

/** modifies string for mine type
** example image/png becomes image
**/
add_filter( ‘facetwp_index_row’, function( $params, $class ) {
if ( ‘post_mime_type’ == $params[‘facet_source’] ) {
$value = $params[‘facet_display_value’];
$value = substr( $value, 0, strpos( $value, ‘/’ ) );
$params[‘facet_value’] = $params[‘facet_display_value’] = $value;
}
return $params;
}, 10, 2 );
{{/gist}}