Gists

1 year ago
// Note that the matching is case sensitive. 
// Do a full re-index anytime you add/edit facetwp_index_row code.
add_filter( 'facetwp_index_row', function( $params, $class ) {
  if ( 'categories' == $params['facet_name'] ) {
    $excluded_terms = array( 'Income', 'Uncategorized' );
    if ( in_array( $params['facet_display_value'], $excluded_terms ) ) {
      return false;
    }
  }
  return $params;
}, 10, 2 );
7 years ago
<?php

// Set the facet's Data source to "Post Type" as a placeholder

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'is_beer_on_tap' == $params['facet_name'] ) {
        if ( is_beer_available( $params['post_id'] ) ) {
            $params['facet_value'] = 'on-tap';
            $params['facet_display_value'] = 'On tap';
        }
        return false; // don't index anything else
    }
    return $params;
}, 10, 2 );
1 week ago
<?php
// Index birthday dates in a custom date field (stored as YYYY-MM-DD) as age.
// This facet can then filter by age range, for example in a Range List facet type:
// https://facetwp.com/help-center/facets/facet-types/range-list/
// E.g.
// under 50
// 50-60
// 60-70
// 70-80
// 80-90
// 90+

add_filter( 'facetwp_index_row', function( $params, $class ) {
  if ( 'my_facet_name' == $params['facet_name'] ) { // change "my_facet_name" to the name of your date-based facet
    $raw_value = $params['facet_value']; // date needs to be in YYYY-MM-DD format

    $birthdate = new DateTime($raw_value);
    $now = new DateTime('now');
    $interval = $birthdate->diff($now);
    $age = $interval->y; // age in years
    
    $params['facet_value'] = $age;
    $params['facet_display_value'] = $age;
  }
  return $params;
}, 10, 2 );
4 weeks ago
<?php

/** indexes only child or deeper level terms **/
add_filter( 'facetwp_index_row', function( $params ) {
    if ( 'product_categories' == $params['facet_name'] ) { //change 'product_categories' to name of your facet
		if ( 1 > $params['depth'] ) { // adjust this for different term levels as needed 
			$params['facet_value'] = ''; // skip indexing
		}
	}
	return $params;
});
4 weeks ago
<?php

/** Indexes all values of an ACF 'endorsement' Checkbox field (set to output an array of 'value : label' choices),
 ** for posts of post type 'csg_brand' that have a company ID field set.
 ** See: https://facetwp.com/help-center/developers/hooks/indexing-hooks/facetwp_index_row/
 */


add_filter( 'facetwp_index_row', function( $params, $class ) {

  // The 'refine_by' facet is set up with 'Post Type' as data source. 'csg_brand' is a post type.
  if ( 'refine_by' == $params['facet_name'] && 'csg_brand' == $params['facet_value'] ) {

    $post_id = $params['post_id'];

    // Get the company ID from the current 'csg_brand' post
    $company_id = get_post_meta( $post_id, 'company', true );
    $company_id = $company_id[0];

    if ( isset( $company_id ) && $company_id > 0 ) {

      $field = get_field_object( 'endorsements', $company_id );
      $checkboxes_values = $field['value'];

      foreach ( $checkboxes_values as $checkbox_value ) {

        if ( is_array( $checkbox_value ) ) {
          $facet_value = FWP()->helper->safe_value( $checkbox_value['value'] );
          $facet_display_value = $checkbox_value['label'];

        } else {
          $facet_value = FWP()->helper->safe_value( $checkbox_value );
          $facet_display_value = $checkbox_value;
        }

        $params['facet_value'] = $facet_value;
        $params['facet_display_value'] = $facet_display_value;
        $class->insert( $params );

      }

      $params['facet_value'] = ''; // skip the original row

    } else {

      // No valid company ID found, set facet_value to empty to avoid indexing
      $params['facet_value'] = ''; // skip indexing
    }

  }

  return $params;

}, 10, 2 );
2 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);
3 months ago
<?php
/**
 ** Facet automatically indexes latitude and longitude from an ACF map field
 ** This will index other data saved in other sub fields
 ** Note that these fields are optional and may not exist
 ** See ACF docs for more - https://www.advancedcustomfields.com/resources/google-map/#template-usage
 **/

add_filter( 'facetwp_index_row', function( $params, $class ) {
	if ( 'my_facet' == $params['facet_name'] ) { // change my_facet to name of your facet
		$post_id = $params['post_id'];
		$acf = str_replace( 'acf/', '', $params['facet_source'] );
		$location = get_field( $acf, $post_id );
		$value = $location['city'] ?? ''; 
		if ( '' != $value ) {
			$params['facet_value'] = $value;
			$params['facet_display_value'] = $value;
		} else {
			$params['facet_value'] = ''; // don't index
		}
	}
	return $params;
}, 10, 2 );
4 months ago
<?php
// Choose the Post Date (or another date field) as the Slider facet's data source.
// Replace "my_slider_facet_name" with the name of your Slider facet.
// Do a full re-index.
// See: https://facetwp.com/help-center/facets/facet-types/slider/#create-a-year-slider

add_filter( 'facetwp_index_row', function( $params, $class ) {
  if ( 'my_slider_facet_name' == $params['facet_name'] ) { 
    $raw_value = $params['facet_value'];
    $params['facet_value'] = date( 'Y', strtotime( $raw_value ) );
    $params['facet_display_value'] = $params['facet_value'];
  }
  return $params;
}, 10, 2 );
5 months ago
<?php
/** changes depth and parent_ids
 ** to allow a hierarchy to start with
 ** first level children instead of top level parents
 **/

add_filter( 'facetwp_index_row', function( $params ) {
    if ( 'my_facet' == $params['facet_name'] ) { // 'my_facet' should be replaced with your facet name
        if ( 1 > $params[ 'parent_id' ] ) { // this will be top level parents
            $params[ 'facet_value' ] = ''; // don't index
        } elseif ( 2 > $params[ 'depth' ] ) { // first level children need depth and parent id changed
            $params[ 'parent_id' ] = 0;
            $params[ 'depth' ] = 0;
        } else { // other children just need depth adusted
            $params[ 'depth' ] = $params[ 'depth' ] - 1;
        }
    }
    return $params;
});
5 months ago
<?php
/** change names formatted "John Smith" 
 ** to display as "Smith, John"
 ** in facet's index
 **/

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'personfacet' == $params['facet_name'] ) { // change 'personfacet' to name of your facet
        $name = $params['facet_display_value'];
        $name = explode( ' ', $name );
        $display_name = array_pop( $name ) . ', ' . implode( ' ', $name );
        $params['facet_display_value'] = $display_name;
    }
    return $params;
}, 10, 2 );