Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Last active April 28, 2021 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgibbs189/1e2f4674ddfeeb8ed7f89e0523c60052 to your computer and use it in GitHub Desktop.
Save mgibbs189/1e2f4674ddfeeb8ed7f89e0523c60052 to your computer and use it in GitHub Desktop.
FacetWP - create a dropdown to filter by "min_beds" (minimum range)
<?php
/*
* Facet name: beds
* Facet type: dropdown
* Data source: "min_beds"
*
* This hook will index all values between min_beds and max_beds. If min_beds = 2 and max_beds = 5,
* then the values "2", "3", "4" and "5" will get indexed for this post.
*/
add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) {
if ( 'beds' == $params['facet']['name'] ) {
$post_id = (int) $params['defaults']['post_id'];
$min_beds = (int) get_field( 'min_beds', $post_id );
$max_beds = (int) get_field( 'max_beds', $post_id );
$rows = [];
for ( $i = $min_beds; $i <= $max_beds; $i++ ) {
$new_row = $params['defaults'];
$new_row['facet_value'] = $i;
$new_row['facet_display_value'] = $i;
$rows[] = $new_row;
}
}
return $rows;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment