Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
facetwp facetwp_facet_filter_posts hook
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.