Gists

by mgibbs189 - 9 months ago

<?php

add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) {
    $defaults = $params['defaults'];
    $post_id = (int) $defaults['post_id'];

    if ( 0 === strpos( $defaults['facet_source'], 'cf/attribute_pa_' ) ) {
        if ( 'product_variation' == get_post_type( $post_id ) ) {
            $parent_id = wp_get_post_parent_id( $post_id );
            $tax = str_replace( 'cf/attribute_', '', $defaults['facet_source'] );
            $terms = get_the_terms( $parent_id, $tax );

            if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
                foreach ( $terms as $term ) {
                    $temp = $defaults;
                    $temp['term_id'] = $term->term_id;
                    $temp['facet_value'] = $term->slug;
                    $temp['facet_display_value'] = $term->name;
                    $temp['variation_id'] = $post_id;
                    $temp['post_id'] = $parent_id;
                    $rows[] = $temp;
                }
            }
        }
    }

    return $rows;
}, 999, 2 );

by mgibbs189 - 11 months ago

<?php

add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) {
    if ( 'locations' == $params['facet']['name'] ) {
        $post_id = (int) $params['defaults']['post_id'];
        $location = get_field( 'locations', $post_id ); // returns an post object
        $field_value = get_field( 'postcode', $location->ID );
        $new_row = $params['defaults'];
        $new_row['facet_value'] = $field_value; // value
        $new_row['facet_display_value'] = $field_value; // label
        $rows[] = $new_row;
    }
    return $rows;
}, 10, 2 );
by mgibbs189 - 11 months ago

<?php

add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) {
    if ( 'keyword' == $params['facet']['name'] ) {
        $new_row = $params['defaults'];
        $post_id = (int) $new_row['post_id'];
        $new_row['facet_value'] = $post_id,
        $new_row['facet_display_value'] = get_the_title( $post_id );
        $rows[] = $new_row;
    }
    return $rows;
}, 10, 2 );
by djrmom - 1 year ago

<?php

/** handles indexing when a woocommerce variation has "any" selected instead of specific values **/
add_filter( 'facetwp_indexer_row_data', function( $rows, $args ) {

    if ( 0 === strpos( $args['defaults']['facet_source'], 'cf/attribute_pa_' ) ) {
        foreach ( $rows AS $row ) {
            if ( 'product_variation' == get_post_type( $row['post_id'] ) && '' == $row['facet_value'] ) {
                $parent = wp_get_post_parent_id( $row['post_id'] );
                if ( 0 === strpos( $row['facet_source'], 'cf/attribute_pa_' ) ) {
                    $taxonomy = str_replace( 'cf/attribute_', '', $row['facet_source'] );
                    $terms = get_the_terms( $parent, $taxonomy );
                    if ( !is_wp_error( $terms ) && !empty( $terms ) ) {
                        foreach ( $terms AS $term ) {
                            $params = $row;
                            $params['term_id'] = $term->term_id;
                            $params['facet_display_value'] = $term->name;
                            $params['facet_value'] = $term->slug;
                            $params['variation_id'] = $row['post_id'];
                            $params['post_id'] = $parent;
                            $rows[] = $params;
                        }
                    }
                }
            }
        }
    }

    return $rows;

}, 11,2 );
by djrmom - 2 years ago

<?php

add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) {
	if ( 'cases_topics' == $params['facet']['name'] ) {
		$post_id = $params['defaults']['post_id'];
		$tags = get_tags( $post_id );
		if ( !empty( $tags ) ) {
			foreach ( $tags as $tag ) {  
				$new_row = $params['defaults'];
				$new_row['facet_source'] = 'tax/post_tag';
				$new_row['facet_value'] = $tag->slug;
				$new_row['facet_display_value'] = $tag->name;
				$new_row['term_id'] = $tag->term_id;
				$rows[] = $new_row;
			}
		}
	}
	return $rows;
}, 10, 2 );
by djrmom - 3 years ago

<?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 );
by djrmom - 3 years ago

<?php
/** adds a "View All" index value to every post
 ** change 'my_radio' to the name of the radio facet to apply to
 ** 'View All' can be changed to whatever label is preferred
 ** reindex after add this code
 **/

add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) {
  if ( 'my_radio' == $params['facet']['name'] ) {
    $new_row = $params['defaults'];
    $new_row['facet_value'] = 'all';
    $new_row['facet_display_value'] = 'View All';
    $rows[] = $new_row;
  }
  return $rows;
}, 10, 2 );