Last active
April 28, 2021 15:17
-
-
Save mgibbs189/1e2f4674ddfeeb8ed7f89e0523c60052 to your computer and use it in GitHub Desktop.
FacetWP - create a dropdown to filter by "min_beds" (minimum range)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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