Gists

by djrmom - 2 years ago

<?php

/** filter to return post_ids from a multi-select, narrow results facet **/
add_filter( 'facetwp_facet_filter_posts', function( $return, $params ) {

	if ( 'product_categories' == $params['facet']['name'] ) {
	    
		global $wpdb;

		$return          = array();
		$facet           = $params['facet'];
		$selected_values = $params['selected_values'];

		$sql = $wpdb->prepare( "SELECT DISTINCT post_id
            FROM {$wpdb->prefix}facetwp_index
            WHERE facet_name = %s",
			$facet['name']
		);

		// Match ALL values
		foreach ( $selected_values as $key => $value ) {
			$results = facetwp_sql( $sql . " AND facet_value IN ('$value')", $facet );
			$return  = ( $key > 0 ) ? array_intersect( $return, $results ) : $results;

			if ( empty( $return ) ) {
				break;
			}
		}

	}

	return $return;
}, 10, 2 );
by mgibbs189 - 4 years ago

<?php

function custom_slider_logic( $response, $params ) {
    $facet = $params['facet'];

    // Change the facet name
    if ( 'YOUR_FACET_NAME' == $facet['name'] ) {
        global $wpdb;

        $values = $params['selected_values'];
        $where = '';

        // Min value - CUSTOMIZE ME
        if ( !empty( $values[0] ) ) {
            $where .= " AND CAST(facet_value AS DECIMAL(10,2)) >= '{$values[0]}'";
        }
        // Max value - CUSTOMIZE ME
        if ( !empty( $values[1] ) ) {
            $where .= " AND CAST(facet_display_value AS DECIMAL(10,2)) <= '{$values[1]}'";
        }

        $sql = "
        SELECT DISTINCT post_id FROM {$wpdb->prefix}facetwp_index
        WHERE facet_name = '{$facet['name']}' $where";
        return $wpdb->get_col( $sql );
    }

    return $response;
}
add_filter( 'facetwp_facet_filter_posts', 'custom_slider_logic', 10, 2 );

by mgibbs189 - 4 years ago

<?php

function fwp_max_capacity( $return, $params ) {
    $facet_name = $params['facet']['name'];

    if ( 'max_capacity' == $facet_name ) {
        $values = $params['selected_values'];
        $max_capacity = ( '' == $values[0] ) ? false : $values[0];

        global $wpdb;

        if ( false !== $max_capacity ) {
            $where .= " AND (facet_value + 0) >= '$max_capacity'";
        }

        $sql = "
        SELECT DISTINCT post_id FROM {$wpdb->prefix}facetwp_index
        WHERE facet_name = '$facet_name' $where";
        return $wpdb->get_col( $sql );
    }
    return $return;
}
add_filter( 'facetwp_facet_filter_posts', 'fwp_max_capacity', 10, 2 );
by mgibbs189 - 4 years ago

<?php

function fwp_match_exact_day( $return, $params ) {
    $facet = $params['facet'];
    $values = $params['selected_values'];

    if ( 'YOUR_FACET_NAME' == $facet['name'] ) {
        global $wpdb;

        $where = '';
        if ( '' != $values[0] ) {
            $where .= " AND LEFT(facet_value, 10) = '{$values[0]}'";
        }

        $sql = "
        SELECT DISTINCT post_id FROM {$wpdb->prefix}facetwp_index
        WHERE facet_name = '{$facet['name']}' $where";
        return $wpdb->get_col( $sql );
    }
    return $return;
}
add_filter( 'facetwp_facet_filter_posts', 'fwp_match_exact_day', 10, 2 );