Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Last active February 11, 2020 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgibbs189/81baf175d8804b08f7d25db3bbfcda6f to your computer and use it in GitHub Desktop.
Save mgibbs189/81baf175d8804b08f7d25db3bbfcda6f to your computer and use it in GitHub Desktop.
FacetWP - index variation attribute with "Any" select
<?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 );
@snaper9
Copy link

snaper9 commented Feb 11, 2020

Line 8 : get_post_tyoe should be get_post_type

@mgibbs189
Copy link
Author

Thanks, fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment