Gists

by mgibbs189 - 3 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 );

by mgibbs189 - 2 weeks ago

<?php

// Add the following to your (child) theme's functions.php

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'region_partner' == $params['facet_name'] ) {
        $params['facet_name'] = 'region_dropdown'; // trick FacetWP into saving values into the other facet
    }
    return $params;
}, 10, 2 );

by djrmom - 1 month ago

<?php

/**
 * reindex after adding or updating this filter
 */
add_filter( 'facetwp_index_row', function( $params, $class ) {
	if ( 'my_facet' == $params['facet_name'] ) { // change my_facet to your facet's name/slug
		if ( '1' == $params['facet_value'] ) { // be careful with 1 and 0 as values - https://www.php.net/manual/en/types.comparisons.php
            $params['facet_display_value'] == 'My Label'; // text to display in the facet choices
        }
	}
	return $params;
}, 10, 2 );

by mgibbs189 - 5 days ago

<?php

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'job_company' == $params['facet_name'] ) {
        if ( ! empty( $params['facet_value'] ) ) {
            $post = get_post( (int) $params['facet_value'] );
            $params['facet_value'] = $post->post_name;
        }
    }
    return $params;
}, 10, 2 );

by mgibbs189 - 3 months ago

<?php

// Add the following to your (child) theme's functions.php file
// Then re-index afterwards

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'country' == $params['facet_name'] && 2 == strlen( $params['facet_value'] ) ) {
        $params['facet_display_value'] = locale_get_display_region( '-' . $params['facet_value'], 'en' );
    }
    return $params;
}, 10, 2 );

by mgibbs189 - 4 months ago

<?php

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

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'YOUR_FACET_NAME' == $params['facet_name'] ) {
        $params['facet_display_value'] = str_replace( '&', '&', $params['facet_display_value'] );
        $params['facet_display_value'] = str_replace( '&', '&', $params['facet_display_value'] );
        $params['facet_display_value'] = str_replace( '&', '&', $params['facet_display_value'] );
    }
    return $params;
}, 10, 2 );

by mgibbs189 - 4 months ago

<?php

/**
 * This will trick FacetWP's indexer into indexing "tags_list" values for the "categories_new" facet
 *
 * Re-index after adding the following code into your (child) theme's functions.php
 */
add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'tags_list' == $params['facet_name'] ) {
        $params['facet_name'] = 'categories_new';
    }
    return $params;
}, 99, 2 );

by mgibbs189 - 6 months ago

<?php

/**
 * Add to your (child) theme's functions.php
 *
 * This assumes a facet named "color" based on an ACF checkboxes field
 * Each line of the ACF field's "Choices" setting is formatted like: #ff0000 : Red
 *
 * Here we're just stripping the "#" so FacetWP won't automatically hash the value
 */

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'color' == $params['facet_name'] ) {
        $params['facet_value'] = str_replace( '#', '', $params['facet_value'] );
    }
    return $params;
}, 10, 2 );
by mgibbs189 - 7 months ago

<?php

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

add_filter( 'facetwp_index_row', function( $params, $class ) {
    $term_id = (int) $params['term_id'];
    $display_value = $params['facet_display_value'];

    // If this is a taxonomy term and the display value is numeric,
    // Then set facet_value = facet_display_value
    if ( 0 < $term_id && is_numeric( $display_value ) ) {
        $params['facet_value'] = $display_value;
    }

    return $params;
}, 10, 2 );

by mgibbs189 - 9 months ago

<?php
/**
 * Setup instructions:
 *
 * 1. Add the following code to your (child) theme's functions.php
 * 2. Create a new facet named "location"
 * 3. Set the facet type to "Proximity"
 * 4. Set the facet's Data source to "Post Type" (as a placeholder)
 * 5. Save and re-index
 */

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'location' == $params['facet_name'] ) {
        global $wpdb;

        $post_id = (int) $params['post_id'];
        $row = $wpdb->get_row( "SELECT latitude, longitude FROM {$wpdb->prefix}gmw_locations WHERE object_type = 'post' and object_id = '$post_id' LIMIT 1" );
        if ( null !== $row ) {
            $params['facet_value'] = $row->latitude;
            $params['facet_display_value'] = $row->longitude;
        }
    }
    return $params;
}, 10, 2 );