11 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 );
2 weeks 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 weeks 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 );
2 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;
});
2 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 );
3 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);
4 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);
6 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 );
9 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;
});