Skip to content

Instantly share code, notes, and snippets.

@djrmom
Created August 29, 2017 21:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save djrmom/239710c0f273ac3cb3781635c7409003 to your computer and use it in GitHub Desktop.
Save djrmom/239710c0f273ac3cb3781635c7409003 to your computer and use it in GitHub Desktop.
facetwp add a taxonomy type source
<?php
/**
* add additional source to select as Taxonomy Types
*/
add_filter( 'facetwp_facet_sources', function( $sources ) {
$sources['posts']['choices']['tax_type'] = 'Taxonomy Types';
return $sources;
}, 10);
/**
* Create custom row indexer
*/
add_filter( 'facetwp_indexer_row_data', function( $rows, $args ) {
if ( 'tax_type' == $args['defaults']['facet_source'] ) {
$taxes = get_post_taxonomies( $args['defaults']['post_id'] );
if ( empty( $taxes ) ) {
return false;
}
$output = array();
foreach ( $taxes as $tax ) {
$terms = wp_get_post_terms( $args['defaults']['post_id'], $tax );
if ( is_wp_error( $terms ) || empty( $terms ) ) {
continue;
}
$tax_type = get_taxonomy( $tax );
if ( ! $tax_type->public ) {
continue;
}
$params = $args['defaults'];
$params['facet_value'] = $tax;
$params['facet_display_value'] = esc_html( $tax_type->label );
$output[] = $params;
}
return $output;
}
return $rows;
}, 10, 2 );
@braddalton
Copy link

I don't think you need this anymore as support for custom taxonomy types are included in the data source settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment