Gists

12 months 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 );
3 weeks 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);
2 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 );
2 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 );
3 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;
});
3 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 );
4 months ago
<?php

// Combines specific choices in a facet that has its data source set to a 'farbe' (color) global product attribute.
// The value is combined with another global attribute 'holzart' (wood type) for certain holzart-farbe combinations.
// E.g. products with the global attributes 'Eiche' and 'Farblos', and 'Buche' and 'Farblos', need to be indexed as 'NATUR'.
// All other combinations need to be indexed with the holzart and farbe values, e.g. 'Esche Farblos'.
// Note: this works for simple products, not variation products.

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

  if ('farbigkeit' == $params['facet_name']) {

    $product_id = $params['post_id'];
    $pa_farbe_name = $params['facet_display_value']; // The facet has data source 'pa_farbe' already, so it alrady has the display value

    // Get the global attribute term name  
    $product = wc_get_product( $product_id );
    $product_attributes = $product->get_attributes();
    $pa_holzart_id = $product_attributes['pa_holzart']['options']['0']; // returns the ID of the term
    $pa_holzart_name = get_term($pa_holzart_id)->name; // returns the term name

    // Custom logic for combining "Holzart" and "Farbe" into "NATUR"
    if ( ('Eiche' == $pa_holzart_name && 'Farblos' == $pa_farbe_name) || ('Buche' == $pa_holzart_name && 'Farblos' == $pa_farbe_name) ) {
      $combined_display_value = 'NATUR';
      $combined_value = 'natur';
    } else {
      $combined_display_value = $pa_holzart_name . ' ' . $pa_farbe_name; // Set the display value as e.g. 'Esche Farblos'
      $combined_value = str_replace(' ', '_', strtolower($combined_display_value)); // Set the technical value as e.g. esche_farblos (lowercase and spaces replaced with underscores)
    }

    $params['facet_display_value'] = $combined_display_value;
    $params['facet_value'] = $combined_value;

  }
  return $params;
}, 10, 2);
5 months ago
<?php

/**
 * Setup:
 * Post Type A has an ACF Relationship field related to Post Type B.
 * A facet has this relationship field set as source.
 * A related Post B can be set to draft, after which the Post A needs to be removed from FacetWP's index table for the facet.
 * FacetWP does not index draft posts, but as this is not a save or edit of Post A, this re-index will not happen automatically.
 * The following will do this:
 **/


// Don't index a Post A if the post_status of the related Post B is not 'publish':
add_filter( 'facetwp_index_row', function( $params, $class ) {
  if ( 'my_facet_name' == $params['facet_name'] ) { // Change 'my_facet_name' to name of your facet
    if ( 'publish' != get_post_status( $params[ 'facet_value' ] ) ) {
      $params[ 'facet_value' ] = ''; // Skip indexing
    }
  }
  return $params;
}, 10, 2 );

// Remove the facet choices (being related Posts B) from FacetWP's index table for Posts A if the status of a Post B is changed to something else than 'publish'.
// See: https://developer.wordpress.org/reference/hooks/transition_post_status/
add_action( 'transition_post_status', function( $new_status, $old_status, $post ) {
  if ( 'publish' != $new_status && 0 < $post->ID ) {
    global $wpdb;
    // Change 'my_facet_name' to name of your facet.
    $sql = "
            DELETE FROM {$wpdb->prefix}facetwp_index
            WHERE facet_value = $post->ID AND facet_name = 'my_facet_name'
        ";
    $wpdb->query( $sql );
  }
}, 10, 3);
8 months ago
<?php

// A facet with its data source set to "Post Author" will display the author names as choices.
// To display the author nicknames instead, add the following code to your (child) theme's functions.php and re-index.
// For other author meta info you can get with get_the_author_meta() and index/display instead of nicknames, see: 
// https://developer.wordpress.org/reference/functions/get_the_author_meta/

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

if ( 'post_author' == $params['facet_name'] ) { // replace 'post_author' with the name of your facet
    $user_id = $params['facet_value'];
    $nickname = get_the_author_meta( 'nickname', $user_id );
    if ( isset( $nickname ) ) {
      $params['facet_display_value'] = $nickname;
    }   
  }

  return $params;
}, 10, 2 );