djrmom
8/29/2017 - 9:02 PM

facetwp add a taxonomy type source

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 );