Gists

2 months ago
<?php
/** change 'Default sort' to the text of your Default label setting of your search facet, be sure to match exactly including case
 ** https://facetwp.com/help-center/facets/facet-types/sort/#available-options
 ** change 'my_search_facet' to the name of your search facet (2 places)
 ** change 'Relevance' to whatever you want the default label to be when using a search facet
 **/
add_filter( 'facetwp_i18n', function( $text ) {
    if ( 'Default sort' == $text && isset( FWP()->facet->facets['my_search_facet']['selected_values'] ) && !empty( FWP()->facet->facets['search']['selected_values'] )  ) {
        return 'Relevance';
    }
    return $text;
});
1 year ago
<?php
// Each facet's source now can be accessed in JavaScript with FWP.settings.facet_sources['your_facet_name']

add_filter( 'facetwp_render_output', function( $output, $params ) {
  $sources = [];

  foreach ( FWP()->facet->facets as $facet_name => $facet_data ) {
    if ( isset ( $facet_data['source'] ) ) {
      $sources[ $facet_name ] = $facet_data['source'];
    }
  }

  $output['settings']['facet_sources'] = $sources;
  return $output;
}, 10, 2 );
1 year ago
<?php

add_filter( 'facetwp_facet_where', function( $where_clause, $facet ) {
    if ( 'categories_checkbox' == $facet['name'] ) {
        $name = $facet['name'];
        $tax = str_replace( 'tax/', '', $facet['source'] );
        $facets = FWP()->facet->facets;

        if ( isset( $facets[ $name ] ) ) {
            if ( ! empty( $facets[ $name ]['selected_values'] ) ) {
                $term_slug = $facets[ $name ]['selected_values'][0];
                $term = get_term_by( 'slug', $term_slug, $tax );
                if ( isset( $term->term_id ) ) {
                    $where_clause .= " AND f.parent_id = '{$term->term_id}'";
                }
            }
        }
    }
    return $where_clause;
}, 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;
});
6 months ago
<?php
/**
 * Modify the output HTML and highlighting all search terms in a Listing Builder template
 */
add_filter( 'facetwp_render_output', function( $output, $params ) {
    $keywords = '';

    if ( isset( FWP()->facet->facets['search'] ) ) {
        $keywords = FWP()->facet->facets['search']['selected_values'];
        $keywords = is_array( $keywords ) ? implode( ' ', $keywords ) : $keywords;
    }

    // SearchWP 4
    if ( ! empty( $keywords ) && class_exists( '\SearchWP\Highlighter' ) ) {
      $highlighter = new \SearchWP\Highlighter();
      $output['template'] = $highlighter->apply( $output['template'], $keywords );
    }
    
    // SearchWP 3    
    // if ( ! empty( $keywords ) && class_exists( 'SearchWPHighlighter' ) ) {
      // $highlighter = new SearchWPHighlighter();
      // $output['template'] = $highlighter->apply_highlight( $output['template'], $keywords );
    // }
    
    return $output;
}, 10, 2 );
2 years ago
<?php
/**
 ** displays alternate html when no posts are found
 **/
add_filter( 'facetwp_template_html', function(  $output, $class ) {
    if ( $class->query->found_posts < 1 ) {
        $output = 'No posts found';
        // add below if you want to output any facets from the filters
        // change my_facet_name to the name of your facet
        if ( isset( FWP()->facet->facets['my_facet_name'] ) ) {
            $keywords = FWP()->facet->facets['my_facet_name']['selected_values'];
            $keywords = is_array( $keywords ) ? implode( ' ', $keywords ) : $keywords;
        }
        if ( !empty( $keywords ) ) {
            $output .= ' for ' . $keywords;
        }
    }
    return $output;
}, 10, 2);
1 year ago
<?php

function fwp_limit_subcategory( $where_clause, $facet ) {
    if ( 'unter_categories' == $facet['name'] ) {

        // See if a category is selected
        foreach ( FWP()->facet->facets as $f ) {
            if ( 'categories' == $f['name'] && ! empty( $f['selected_values'] ) ) {
                $term_slug = $f['selected_values'][0];
                $term = get_term_by( 'slug', $term_slug, 'category' );
                if ( isset( $term->term_id ) ) {
                    $where_clause .= " AND f.parent_id = '{$term->term_id}'";
                    break;
                }
            }
        }
    }
    return $where_clause;
}
add_filter( 'facetwp_facet_where', 'fwp_limit_subcategory', 10, 2 );