Gists

3 months ago
<?php
// A scenario with 2 facets ('locaties' and 'provincie') that both use the same taxonomy 'regio' as data source.
// The provincie facet is set up to index and show only the child terms level of the terms shown in the locaties facet, with the first 'facetwp_index_row' hook snippet
// If a parent term is selected in the locaties facet, the provincie facet will show all child terms in that level, not necessarily direct child terms of the selected parent term in the locaties facet.
// The second snippet makes sure the provincie facet only shows *direct* child terms of the choice selected in the locaties facet.

// Index only the specific depth in the provincie facet.
// Needs re-indexing to work.
add_filter('facetwp_index_row', function($params) {
  if ('provincie' == $params['facet_name']) {
    // Get the ancestors of the term
    $parents = get_ancestors($params['term_id'], 'regio', 'taxonomy');

    // Check if the term is exactly two layers deep
    if (count($parents) !== 2) {
      // If not, set facet_value to an empty string
      $params['facet_value'] = '';
    }
  }
  return $params;
});

// Let the provincie facet only show direct child terms of the choice selected in the locaties facet:
add_filter( 'facetwp_facet_html', function( $output, $params ) {

  // Check if the facet is 'provincie'
  if ('provincie' == $params['facet']['name']) {
    // Get the facet values
    $values = $params['values'];

    // Get the selected value of the 'locaties' facet  
    $selected_value = FWP()->facet->facets['locaties']['selected_values'][0];

    // If the selected value is not null, get the term ID
    if (!is_null($selected_value)) {
      // Get the term by its slug
      $term = get_term_by('slug', $selected_value, 'regio');

      // If the term exists, store the term ID in the global variable
      if ($term) {
        $parent_term_id = $term->term_id;
      }
    }

    // Filter the values based on the parent term
    $filtered_values = array_filter($values, function($value) use ($parent_term_id) {
      // Check if the value's parent is the specified parent term
      return $value['parent_id'] == $parent_term_id;
    });

    // Generate the new facet HTML
    $output = '';
    $selected_values = (array) $params['selected_values'];
    foreach ($filtered_values as $value) {
      $label = esc_html( $value['facet_display_value'] );
      $selected = in_array( $value['facet_value'], $selected_values ) ? ' checked' : '';
      $selected .= ( '' != $value['counter'] && 0 == $value['counter'] && '' == $selected ) ? ' disabled' : '';
      $output .= '<div class="facetwp-checkbox' . $selected . '" data-value="' . esc_attr( $value['facet_value'] ) . '">';
      $output .= '<span class="facetwp-display-value">';
      $output .= apply_filters( 'facetwp_facet_display_value', $label, [
        'selected' => ( '' !== $selected ),
        'facet' => $params['facet'],
        'row' => $value
      ]);
      $output .= '</span>';
      $output .= '<span class="facetwp-counter">(' . $value['counter'] . ')</span>';
      $output .= '</div>';
    }
  }

  return $output;
},10,2);
6 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;
});
2 years 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 );
2 years 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;
});
11 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);
2 years 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 );