Gists

4 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 );
1 year ago
<?php

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

add_filter( 'facetwp_facet_filter_posts', function( $result, $params ) {
    if ( 'categories' == $params['facet']['name'] ) {
        return 'continue'; // prevent facet from being processed
    }
    return $result;
}, 10, 2 );
6 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 );