Gists

by djrmom - 8 months ago

<?php

/** on a taxonomy term page remove terms from facet that are not the same or a sub category of that term
 * example is for woocommerce product categories
 */
add_filter( 'facetwp_facet_render_args', function( $args ) {
    if ( 'product_categories' == $args['facet']['name'] && is_tax( 'product_cat' ) ) {
        $current_term = get_queried_object_id();
        if ( ! empty( $args['values'] ) ) {
            foreach ( $args['values'] as $key => $val ) {
                $term_id = $val['term_id'];
                // this keeps sub-terms of the current term archive page
                if ( $val['parent_id'] && in_array( $current_term, get_ancestors( $val['term_id'], 'product_cat', 'taxonomy' ) ) ) {
                    continue;
                // this keeps term for current archive page, delete this else if to if not wanting the current term also
                } else if ( $current_term == $val['term_id'] ) {
                        continue;
                // this removes non-matching terms from the choices for the facet.
                } else {
                    unset( $args['values'][$key] );
                }
            }
            $args['values'] = array_values( $args['values'] );
        }
    }
    return $args;
});
by djrmom - 2 years ago

<?php

add_filter( 'facetwp_facet_render_args', function( $args) {
	if ( 'my_search_facet' == $args['facet']['name'] ) {  // change my_search_facet to name of your facet
		$args['facet']['placeholder'] = 'Something';
	}
	return $args;
} );
by djrmom - 3 years ago

//filter color display to maybe add hash to hex colors
add_filter( 'facetwp_facet_render_args', function( $args ) {
	if ( 'colors' == $args['facet']['name'] ) {
	    foreach ( $args['values'] AS $key => $value ) {
	        $args['values'][$key]['facet_display_value'] = ( '' != ( $color = sanitize_hex_color_no_hash( $value['facet_display_value'] ) ) ) ? '#' . $color : $value['facet_display_value'];
        }
	}
	return $args;
});

//filter color index to maybe add hash to hex colors
add_filter( 'facetwp_index_row', function( $params ) {
	if ( 'colors' == $params['facet_name'] ) {
		$display_value = $params['facet_display_value'];
		$params['facet_display_value'] = ( '' != ( $color = sanitize_hex_color_no_hash( $params['facet_display_value'] ) ) ) ? '#' . $color : $params['facet_display_value'];
	}
	return $params;
}, 10 );