Gists

5 months ago
<?php
/** enable hierarchy on an existing taxonomy **/
add_filter( 'register_taxonomy_args', function( $args, $taxonomy ) {
    if ( 'pa_color' == $taxonomy ) {
        $args['hierarchical'] = true;
    }
    return $args;
}, 10, 2 );
4 months ago
<?php
/**
 * An opposite/negative/excluding index for taxonomies
 * For example if recipes are tagged "gluten" to indicate an allergy, this will index everything
 * not tagged gluten so that selecting a facet choice gluten allergen would display all recipes without gluten
 * Only for taxonomies
 * Ignores hierarchy, ignores modifier settings in facet
 * Re-index after adding this code
 */
add_filter( 'facetwp_indexer_post_facet', function( $bypass, $params ) {
    if ( 'my_categories' == $params['facet']['name'] ) { // 'my_categories' change to name of facet

        $post_id = $params['defaults']['post_id'];

        /** checks post type, otherwise post types that don't have this taxonomy would get indexed for all terms */
        if ( 'product' !== get_post_type( $post_id ) ) { // change 'product' to your post type
            return true; // skip indexes this facet
        }

        /** this assumes the datasource is a taxonomy, it can just be hardcoded instead of checking the facet setting and extracting the name */
        $taxonomy = substr( $params['defaults']['facet_source'], 4 ); 

        $terms = get_terms( [ 'taxonomy' => $taxonomy, 'hide_empty' => false ] );

        $post_terms = get_the_terms( $post_id, $taxonomy );
        $post_terms = wp_list_pluck( $post_terms, 'term_id' ); // gets all term ids for this post

        foreach ( $terms as $term ) {
            if ( !in_array( $term->term_id, $post_terms ) ) { // only index terms not set for this post.
                $row = $params['defaults'];
                $row['facet_value'] = $term->slug;
                /** since this is the opposite, for example, you might change the name "Gluten" to "Gluten Free" like this:
                 * $row['facet_display_value'] = $term->name . " Free";
                 */
                $row['facet_display_value'] = $term->name;
                $row['term_id'] = $term->term_id;
                FWP()->indexer->index_row( $row );
            }
        }

        $bypass = true; // skip normal indexing
    }
    return $bypass;
}, 10, 2 );
2 years ago
<?php
/** basic filter for pre-selecting a facet choice that corresponds to the current archive term
 ** ex. http://example.com/category/someterm/
 ** only works when term slug is the same as the facet value and
 ** is the last part of the url, in the above example 'someterm' is the term slug
 ** and will be the facet value when selected ?categories=someterm
 **/

add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
    if ( false !== strpos( FWP()->helper->get_uri(), 'category' ) ) { // change 'category' whatever the url is for the term archive
        if ( empty( $url_vars['categories'] ) ) { // change 'categories' to name of the facet here and below
            $term = basename( FWP()->helper->get_uri() );
            $url_vars['categories'] = [ $term ];
        }
    }
    return $url_vars;
} );
2 years ago
<?php

/**
 * Manually translate taxonomy terms
 * 
 * Change $facet_name
 * Change $tax_name
 */

add_filter( 'facetwp_facet_display_value', function( $label, $params ) {
    $facet_name = 'my_facet_name';
    $tax_name = 'my_tax_name';

	if ( $facet_name == $params['facet']['name'] ) {
		$current = ( !empty( FWP()->facet->http_params['lang'] ) ) ? FWP()->facet->http_params['lang'] :  apply_filters( 'wpml_current_language', null );  
		$default = apply_filters('wpml_default_language', NULL );
		if ( $current != $default ) {
			$translated_id = apply_filters( 'wpml_object_id', $params['row']['term_id'], $tax_name, TRUE, $current );
			$term = get_term( $translated_id );

			if ( ! empty( $term ) ) {
				$label = esc_html( $term->name );
			}
		}        
	}
	return $label;
}, 10, 2 );
3 years ago
<?php

/**
 *
 * This assumes a facet named "product_catalogue" using "product_cats" taxonomy
 *
 * If the facet is in use, it will send to the browser an object containing terms
 * (type `FWP_JSON.product_cat` into the browser console)
 *
 */
add_filter( 'facetwp_assets', function( $assets ) {
    $facet_name = 'product_catalogue';
    $taxonomy = 'product_cat';

    if ( isset( FWP()->facet->facets[ $facet_name ] ) ) {
        $selected = FWP()->facet->facets[ $facet_name ]['selected_values'];

        if ( ! empty( $selected ) ) {
            FWP()->display->json[ $taxonomy ] = FWP()->helper->get_term_depths( $taxonomy );
        }
    }

    return $assets;
});
3 years ago
<?php

// Add the following to your (child) theme's functions.php and re-index afterwards

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'alcohol' == $params['facet_name'] ) {
        $value = $params['facet_display_value'];

        // Swap "," and "." since this is EU-based number format
        $value = str_replace( ',', '{comma}', $value );
        $value = str_replace( '.', ',', $value );
        $value = str_replace( '{comma}', '.', $value );

        $params['facet_value'] = $value;
        $params['facet_display_value'] = $value;
    }

    return $params;
}, 10, 2 );
4 years ago
<?php

// Add the following to your (child) theme's functions.php and re-index afterwards

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'gewicht' == $params['facet_name'] ) {
        $params['facet_value'] = $params['facet_display_value'];
    }
    return $params;
}, 10, 2 );
4 years ago
<?php
/** basic output of using facet links instead of taxonomy archive
 ** change 'my_taxonomy_name', 'fwp_my_facet_name', 'http://example.com/page_for_facets/'
 ** note that if using facetwp_index_row or facetwp_indexer_row_data to change the facetwp_value
 ** this would not work out of the box
 **/
$terms = get_the_terms( get_the_ID(), 'my_taxonomy_name' );
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
  foreach ( $terms AS $term ) {
    $name = $term->name;
    $link = add_query_arg( 'fwp_my_facet_name', FWP()->helper->safe_value( $term->slug ), 'http://example.com/page_for_facets/' );
    echo "<a href='$link'>$name</a><br />"; // very basic output of link, change as need
  }
}
4 years ago
<?php
/**
 * order facets by term order
 * note: facets are an ACF relationship field
 * rather than a direct taxonomy field which
 * prevents use of taxomomy order directly with get_terms_orderby
 */
add_filter( 'facetwp_facet_orderby', function( $orderby, $facet ) {
	// use 'facet_name' => 'taxonomy slug'
	$ordered_facets = [
		'facet_one' => 'tax_one',
		'facet_two' => 'tax_two'
	];
	if (  $ordered_facets[$facet['name']] ) {		
		$terms = get_terms( array(
			'taxonomy' => $ordered_facets[$facet['name']],
    		'hide_empty' => false,
			'orderby' => 'term_order',
			'order' => 'ASC'
		));
		foreach ($terms AS $term ) {
			$termlist[] = $term->slug;
		}
        $orderby = 'FIELD(f.facet_value, "' . implode( '", "', $termlist ) . '" )';
    }
    return $orderby;
}, 11, 2);
2 years ago
<?php

// In the facet's settings, set the 'Sort by' setting to 'Term order'
// If you use the above code and also have WooCommerce installed, make sure you don't try to order WooCommerce taxonomies (like Product categories) with the Custom Taxonomy Order plugin interface. Instead, use the drag-and-drop ordering of terms that comes with WooCommerce itself. The above code excludes the specific way of ordering that WooCommerce uses (by term meta).

add_filter( 'get_terms_args', function( $args, $taxonomies ) {
	if ( isset( $args['term_order'] ) && 'order' !== $args['meta_key'] ) { // The second condition is needed to preserve WooCommerce ordering for product categories, by a termmeta field named "order".
		$args['orderby'] = 'term_order';
	}
	return $args;
}, 10, 2 );

add_filter( 'get_terms_orderby', function( $orderby, $query_vars, $taxonomies ) {
	return 'term_order' === $query_vars['orderby'] ? 'term_order' : $orderby;
}, 10, 3 );