Gists

8 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 );
1 week 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);
1 month 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);
4 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 );
6 months ago
<?php

// index date as mmdd for example 1225
add_filter( 'facetwp_index_row', function( $params, $class ) {
	if ( 'today' == $params['facet_name'] ) { // change today to name of your facet
		$raw_value = $params['facet_value'];
		$params['facet_value'] = date( 'md', strtotime( $raw_value ) );
		$params['facet_display_value'] = date( 'F j', strtotime( $raw_value ) );
	}
	return $params;
}, 10, 2 );

// output only the facet choice for today with label of today, if
// there are none, this will be a ghost with show ghosts enabled
add_filter( 'facetwp_facet_render_args', function( $args ) {
    if ( 'today' == $args['facet']['name'] ) {

        $lookup = [];
        $temp_values = [];
        $days = [ date( 'md' ) ];

        foreach ( $args['values'] as $row ) {
            $lookup[ $row['facet_value'] ] = $row;
        }

        foreach ( $days as $day ) {
            if ( isset( $lookup[ $day ] ) ) {
                $temp_values[] = $lookup[ $day ];
            }
            else {
                $temp_values[] = [
                    'facet_value' => $day,
                    'facet_display_value' => 'Today',
                    'counter' => 0
                ];
            }
        }

        $args['values'] = $temp_values;
    }

    return $args;
});
1 year ago
<?php
add_filter( 'facetwp_index_row', function( $params ) {
    if ( 'my_facet' == $params['facet_name'] ) { // 'my_facet' should be replaced with your facet name
        $parents = get_ancestors( $params['term_id'], 'category', 'taxonomy' ); // change 'category' to name of your taxonomy
        if ( ! in_array( '50', $parents ) ) { // change 50 to term_id of your top level term
            $params[ 'facet_value' ] = '';
        } elseif ( 2 > count( $parents ) ) { // adjust 2 if the parent term is not a top level term, ie 3 for second level term as the parent
            $params[ 'parent_id' ] = 0;
            $params[ 'depth' ] = 0;
        } else {
            $params[ 'depth' ] = $params[ 'depth' ] - 1;
        }
    }
    return $params;
});
1 year ago
<?php

// Add to your (child) theme's functions.php and re-index afterwards

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'audience' == $params['facet_name'] ) {
        if ( 'for-all' == $params['facet_value'] ) {
            $new_row = $params;

            $new_row['facet_value'] = 'for-individual-employees';
            $new_row['facet_display_value'] = 'For Individual Employees';
            $class->insert( $new_row );

            $new_row['facet_value'] = 'for-jobseekers';
            $new_row['facet_display_value'] = 'For Jobseekers';
            $class->insert( $new_row );

            $new_row['facet_value'] = 'for-hr-professionals-team-leaders-anddi-practicion';
            $new_row['facet_display_value'] = 'HR Professionals, Team Leaders and D&I Practitioners';
            $class->insert( $new_row );
        }
    }
    return $params;
}, 10, 2 );
1 year ago
// For facets with post types as source, the name that is shown in the facet's choices is the labels->name set when registering the post type.
// See: https://developer.wordpress.org/reference/functions/register_post_type/
// If you need it to be something else, e.g. labels->singular_name, the following code will index that name.
// Replace 'my_facet_name' with the name of your facet.
// Post type labels array overview: https://developer.wordpress.org/reference/functions/get_post_type_labels/

add_filter( 'facetwp_index_row', function( $params, $class ) {
  $name = $params['facet_name'];
  if ( in_array( $name, [ 'my_facet_name' ] ) ) { // you can add multiple facets to the array
    $post_id = (int) $params['post_id'];
    $post_type_obj = get_post_type_object( get_post_type($post_id) );
    $params['facet_display_value'] = $post_type_obj->labels->singular_name;
  }
  return $params;
}, 10, 2 );
1 year ago
<?php

add_filter( 'facetwp_index_row', function( $params, $class ) {
    $name = $params['facet_name'];

    // Force-index data from one facet into another
    if ( 'search_by_name' == $name ) {
        $params['facet_name'] = 'category';
    }

    // Append the post type to the facet choice label
    if ( in_array( $name, [ 'search_by_name', 'category' ] ) ) {
        $post_id = (int) $params['post_id'];
        $post_type = get_post_type( $post_id );
        $params['facet_display_value'] .= ' (' . $post_type . ')';
    }

    return $params;
}, 10, 2 );