Created
August 29, 2017 21:02
-
-
Save djrmom/239710c0f273ac3cb3781635c7409003 to your computer and use it in GitHub Desktop.
facetwp add a taxonomy type source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't think you need this anymore as support for custom taxonomy types are included in the data source settings.