facetwp facetwp_facet_filter_posts hook

djrmom

{{gist file=”custom-hooks.php” lang=”php”}}
<?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 );
{{/gist}}